]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { values } from 'lodash' |
e02643f3 | 2 | import * as Sequelize from 'sequelize' |
350e31d6 | 3 | import { JobCategory, JobState } from '../../../shared/models/job.model' |
5cd80545 C |
4 | import { JOB_CATEGORIES, JOB_STATES } from '../../initializers' |
5 | import { addMethodsToModel, getSort } from '../utils' | |
6 | import { JobAttributes, JobInstance, JobMethods } from './job-interface' | |
e02643f3 C |
7 | |
8 | let Job: Sequelize.Model<JobInstance, JobAttributes> | |
e4f97bab | 9 | let listWithLimitByCategory: JobMethods.ListWithLimitByCategory |
5cd80545 C |
10 | let listForApi: JobMethods.ListForApi |
11 | let toFormattedJSON: JobMethods.ToFormattedJSON | |
227d02fe | 12 | |
127944aa | 13 | export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
e02643f3 | 14 | Job = sequelize.define<JobInstance, JobAttributes>('Job', |
227d02fe C |
15 | { |
16 | state: { | |
65fcc311 | 17 | type: DataTypes.ENUM(values(JOB_STATES)), |
227d02fe C |
18 | allowNull: false |
19 | }, | |
e4f97bab C |
20 | category: { |
21 | type: DataTypes.ENUM(values(JOB_CATEGORIES)), | |
22 | allowNull: false | |
23 | }, | |
227d02fe C |
24 | handlerName: { |
25 | type: DataTypes.STRING, | |
26 | allowNull: false | |
27 | }, | |
28 | handlerInputData: { | |
29 | type: DataTypes.JSON, | |
30 | allowNull: true | |
31 | } | |
32 | }, | |
33 | { | |
34 | indexes: [ | |
35 | { | |
350e31d6 | 36 | fields: [ 'state', 'category' ] |
227d02fe | 37 | } |
e02643f3 | 38 | ] |
227d02fe C |
39 | } |
40 | ) | |
41 | ||
5cd80545 C |
42 | const classMethods = [ |
43 | listWithLimitByCategory, | |
44 | listForApi | |
45 | ] | |
46 | const instanceMethods = [ | |
47 | toFormattedJSON | |
48 | ] | |
49 | addMethodsToModel(Job, classMethods, instanceMethods) | |
e02643f3 | 50 | |
227d02fe C |
51 | return Job |
52 | } | |
53 | ||
5cd80545 C |
54 | toFormattedJSON = function (this: JobInstance) { |
55 | return { | |
56 | id: this.id, | |
57 | state: this.state, | |
58 | category: this.category, | |
59 | handlerName: this.handlerName, | |
60 | handlerInputData: this.handlerInputData, | |
61 | createdAt: this.createdAt, | |
62 | updatedAt: this.updatedAt | |
63 | } | |
64 | } | |
65 | ||
227d02fe C |
66 | // --------------------------------------------------------------------------- |
67 | ||
350e31d6 | 68 | listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) { |
227d02fe C |
69 | const query = { |
70 | order: [ | |
71 | [ 'id', 'ASC' ] | |
72 | ], | |
73 | limit: limit, | |
74 | where: { | |
350e31d6 C |
75 | state, |
76 | category: jobCategory | |
227d02fe C |
77 | } |
78 | } | |
79 | ||
6fcd19ba | 80 | return Job.findAll(query) |
227d02fe | 81 | } |
5cd80545 C |
82 | |
83 | listForApi = function (start: number, count: number, sort: string) { | |
84 | const query = { | |
85 | offset: start, | |
86 | limit: count, | |
87 | order: [ getSort(sort) ] | |
88 | } | |
89 | ||
90 | return Job.findAndCountAll(query).then(({ rows, count }) => { | |
91 | return { | |
92 | data: rows, | |
93 | total: count | |
94 | } | |
95 | }) | |
96 | } |