1 import { values } from 'lodash'
2 import * as Sequelize from 'sequelize'
3 import { JobCategory, JobState } from '../../../shared/models/job.model'
4 import { JOB_CATEGORIES, JOB_STATES } from '../../initializers'
5 import { addMethodsToModel, getSort } from '../utils'
6 import { JobAttributes, JobInstance, JobMethods } from './job-interface'
8 let Job: Sequelize.Model<JobInstance, JobAttributes>
9 let listWithLimitByCategory: JobMethods.ListWithLimitByCategory
10 let listForApi: JobMethods.ListForApi
11 let toFormattedJSON: JobMethods.ToFormattedJSON
13 export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
14 Job = sequelize.define<JobInstance, JobAttributes>('Job',
17 type: DataTypes.ENUM(values(JOB_STATES)),
21 type: DataTypes.ENUM(values(JOB_CATEGORIES)),
25 type: DataTypes.STRING,
36 fields: [ 'state', 'category' ]
42 const classMethods = [
43 listWithLimitByCategory,
46 const instanceMethods = [
49 addMethodsToModel(Job, classMethods, instanceMethods)
54 toFormattedJSON = function (this: JobInstance) {
58 category: this.category,
59 handlerName: this.handlerName,
60 handlerInputData: this.handlerInputData,
61 createdAt: this.createdAt,
62 updatedAt: this.updatedAt
66 // ---------------------------------------------------------------------------
68 listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) {
80 return Job.findAll(query)
83 listForApi = function (start: number, count: number, sort: string) {
87 order: [ getSort(sort) ]
90 return Job.findAndCountAll(query).then(({ rows, count }) => {