]>
Commit | Line | Data |
---|---|---|
1 | import { values } from 'lodash' | |
2 | import * as Sequelize from 'sequelize' | |
3 | ||
4 | import { JOB_STATES, JOB_CATEGORIES } from '../../initializers' | |
5 | ||
6 | import { addMethodsToModel } from '../utils' | |
7 | import { | |
8 | JobInstance, | |
9 | JobAttributes, | |
10 | ||
11 | JobMethods | |
12 | } from './job-interface' | |
13 | import { JobCategory, JobState } from '../../../shared/models/job.model' | |
14 | ||
15 | let Job: Sequelize.Model<JobInstance, JobAttributes> | |
16 | let listWithLimitByCategory: JobMethods.ListWithLimitByCategory | |
17 | ||
18 | export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | |
19 | Job = sequelize.define<JobInstance, JobAttributes>('Job', | |
20 | { | |
21 | state: { | |
22 | type: DataTypes.ENUM(values(JOB_STATES)), | |
23 | allowNull: false | |
24 | }, | |
25 | category: { | |
26 | type: DataTypes.ENUM(values(JOB_CATEGORIES)), | |
27 | allowNull: false | |
28 | }, | |
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', 'category' ] | |
42 | } | |
43 | ] | |
44 | } | |
45 | ) | |
46 | ||
47 | const classMethods = [ listWithLimitByCategory ] | |
48 | addMethodsToModel(Job, classMethods) | |
49 | ||
50 | return Job | |
51 | } | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
55 | listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) { | |
56 | const query = { | |
57 | order: [ | |
58 | [ 'id', 'ASC' ] | |
59 | ], | |
60 | limit: limit, | |
61 | where: { | |
62 | state, | |
63 | category: jobCategory | |
64 | } | |
65 | } | |
66 | ||
67 | return Job.findAll(query) | |
68 | } |