aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/job/job.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/job/job.ts')
-rw-r--r--server/models/job/job.ts137
1 files changed, 60 insertions, 77 deletions
diff --git a/server/models/job/job.ts b/server/models/job/job.ts
index f428e26db..35c357e69 100644
--- a/server/models/job/job.ts
+++ b/server/models/job/job.ts
@@ -1,96 +1,79 @@
1import { values } from 'lodash' 1import { values } from 'lodash'
2import * as Sequelize from 'sequelize' 2import { AllowNull, Column, CreatedAt, DataType, Model, Table, UpdatedAt } from 'sequelize-typescript'
3import { JobCategory, JobState } from '../../../shared/models/job.model' 3import { JobCategory, JobState } from '../../../shared/models'
4import { JOB_CATEGORIES, JOB_STATES } from '../../initializers' 4import { JOB_CATEGORIES, JOB_STATES } from '../../initializers'
5import { addMethodsToModel, getSort } from '../utils' 5import { getSort } from '../utils'
6import { JobAttributes, JobInstance, JobMethods } from './job-interface'
7 6
8let Job: Sequelize.Model<JobInstance, JobAttributes> 7@Table({
9let listWithLimitByCategory: JobMethods.ListWithLimitByCategory 8 tableName: 'job',
10let listForApi: JobMethods.ListForApi 9 indexes: [
11let toFormattedJSON: JobMethods.ToFormattedJSON
12
13export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
14 Job = sequelize.define<JobInstance, JobAttributes>('Job',
15 {
16 state: {
17 type: DataTypes.ENUM(values(JOB_STATES)),
18 allowNull: false
19 },
20 category: {
21 type: DataTypes.ENUM(values(JOB_CATEGORIES)),
22 allowNull: false
23 },
24 handlerName: {
25 type: DataTypes.STRING,
26 allowNull: false
27 },
28 handlerInputData: {
29 type: DataTypes.JSON,
30 allowNull: true
31 }
32 },
33 { 10 {
34 indexes: [ 11 fields: [ 'state', 'category' ]
35 {
36 fields: [ 'state', 'category' ]
37 }
38 ]
39 } 12 }
40 )
41
42 const classMethods = [
43 listWithLimitByCategory,
44 listForApi
45 ] 13 ]
46 const instanceMethods = [ 14})
47 toFormattedJSON 15export class JobModel extends Model<JobModel> {
48 ] 16 @AllowNull(false)
49 addMethodsToModel(Job, classMethods, instanceMethods) 17 @Column(DataType.ENUM(values(JOB_STATES)))
18 state: JobState
50 19
51 return Job 20 @AllowNull(false)
52} 21 @Column(DataType.ENUM(values(JOB_CATEGORIES)))
22 category: JobCategory
53 23
54toFormattedJSON = function (this: JobInstance) { 24 @AllowNull(false)
55 return { 25 @Column
56 id: this.id, 26 handlerName: string
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 27
66// --------------------------------------------------------------------------- 28 @AllowNull(true)
29 @Column(DataType.JSON)
30 handlerInputData: any
67 31
68listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) { 32 @CreatedAt
69 const query = { 33 creationDate: Date
70 order: [ 34
71 [ 'id', 'ASC' ] 35 @UpdatedAt
72 ], 36 updatedOn: Date
73 limit: limit, 37
74 where: { 38 static listWithLimitByCategory (limit: number, state: JobState, jobCategory: JobCategory) {
75 state, 39 const query = {
76 category: jobCategory 40 order: [
41 [ 'id', 'ASC' ]
42 ],
43 limit: limit,
44 where: {
45 state,
46 category: jobCategory
47 }
77 } 48 }
49
50 return JobModel.findAll(query)
78 } 51 }
79 52
80 return Job.findAll(query) 53 static listForApi (start: number, count: number, sort: string) {
81} 54 const query = {
55 offset: start,
56 limit: count,
57 order: [ getSort(sort) ]
58 }
82 59
83listForApi = function (start: number, count: number, sort: string) { 60 return JobModel.findAndCountAll(query).then(({ rows, count }) => {
84 const query = { 61 return {
85 offset: start, 62 data: rows,
86 limit: count, 63 total: count
87 order: [ getSort(sort) ] 64 }
65 })
88 } 66 }
89 67
90 return Job.findAndCountAll(query).then(({ rows, count }) => { 68 toFormattedJSON () {
91 return { 69 return {
92 data: rows, 70 id: this.id,
93 total: count 71 state: this.state,
72 category: this.category,
73 handlerName: this.handlerName,
74 handlerInputData: this.handlerInputData,
75 createdAt: this.createdAt,
76 updatedAt: this.updatedAt
94 } 77 }
95 }) 78 }
96} 79}