]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { values } from 'lodash' |
e02643f3 | 2 | import * as Sequelize from 'sequelize' |
227d02fe | 3 | |
e4f97bab | 4 | import { JOB_STATES, JOB_CATEGORIES } from '../../initializers' |
227d02fe | 5 | |
74889a71 | 6 | import { addMethodsToModel } from '../utils' |
e02643f3 | 7 | import { |
e02643f3 C |
8 | JobInstance, |
9 | JobAttributes, | |
10 | ||
11 | JobMethods | |
12 | } from './job-interface' | |
ee9e7b61 | 13 | import { JobState } from '../../../shared/models/job.model' |
e02643f3 C |
14 | |
15 | let Job: Sequelize.Model<JobInstance, JobAttributes> | |
e4f97bab | 16 | let listWithLimitByCategory: JobMethods.ListWithLimitByCategory |
227d02fe | 17 | |
127944aa | 18 | export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
e02643f3 | 19 | Job = sequelize.define<JobInstance, JobAttributes>('Job', |
227d02fe C |
20 | { |
21 | state: { | |
65fcc311 | 22 | type: DataTypes.ENUM(values(JOB_STATES)), |
227d02fe C |
23 | allowNull: false |
24 | }, | |
e4f97bab C |
25 | category: { |
26 | type: DataTypes.ENUM(values(JOB_CATEGORIES)), | |
27 | allowNull: false | |
28 | }, | |
227d02fe C |
29 | handlerName: { |
30 | type: DataTypes.STRING, | |
31 | allowNull: false | |
32 | }, | |
33 | handlerInputData: { | |
34 | type: DataTypes.JSON, | |
35 | allowNull: true | |
36 | } | |
37 | }, | |
38 | { | |
39 | indexes: [ | |
40 | { | |
41 | fields: [ 'state' ] | |
42 | } | |
e02643f3 | 43 | ] |
227d02fe C |
44 | } |
45 | ) | |
46 | ||
e4f97bab | 47 | const classMethods = [ listWithLimitByCategory ] |
e02643f3 C |
48 | addMethodsToModel(Job, classMethods) |
49 | ||
227d02fe C |
50 | return Job |
51 | } | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
e4f97bab | 55 | listWithLimitByCategory = function (limit: number, state: JobState) { |
227d02fe C |
56 | const query = { |
57 | order: [ | |
58 | [ 'id', 'ASC' ] | |
59 | ], | |
60 | limit: limit, | |
61 | where: { | |
4e284e97 | 62 | state |
227d02fe C |
63 | } |
64 | } | |
65 | ||
6fcd19ba | 66 | return Job.findAll(query) |
227d02fe | 67 | } |