2017-05-15 15:22:03 -05:00
|
|
|
import { values } from 'lodash'
|
2017-05-22 13:58:25 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-05-02 15:02:27 -05:00
|
|
|
|
2017-11-09 10:51:58 -06:00
|
|
|
import { JOB_STATES, JOB_CATEGORIES } from '../../initializers'
|
2017-05-02 15:02:27 -05:00
|
|
|
|
2017-06-16 02:45:46 -05:00
|
|
|
import { addMethodsToModel } from '../utils'
|
2017-05-22 13:58:25 -05:00
|
|
|
import {
|
|
|
|
JobInstance,
|
|
|
|
JobAttributes,
|
|
|
|
|
|
|
|
JobMethods
|
|
|
|
} from './job-interface'
|
2017-11-14 10:31:26 -06:00
|
|
|
import { JobCategory, JobState } from '../../../shared/models/job.model'
|
2017-05-22 13:58:25 -05:00
|
|
|
|
|
|
|
let Job: Sequelize.Model<JobInstance, JobAttributes>
|
2017-11-09 10:51:58 -06:00
|
|
|
let listWithLimitByCategory: JobMethods.ListWithLimitByCategory
|
2017-05-02 15:02:27 -05:00
|
|
|
|
2017-06-11 10:35:32 -05:00
|
|
|
export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
2017-05-22 13:58:25 -05:00
|
|
|
Job = sequelize.define<JobInstance, JobAttributes>('Job',
|
2017-05-02 15:02:27 -05:00
|
|
|
{
|
|
|
|
state: {
|
2017-05-15 15:22:03 -05:00
|
|
|
type: DataTypes.ENUM(values(JOB_STATES)),
|
2017-05-02 15:02:27 -05:00
|
|
|
allowNull: false
|
|
|
|
},
|
2017-11-09 10:51:58 -06:00
|
|
|
category: {
|
|
|
|
type: DataTypes.ENUM(values(JOB_CATEGORIES)),
|
|
|
|
allowNull: false
|
|
|
|
},
|
2017-05-02 15:02:27 -05:00
|
|
|
handlerName: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
handlerInputData: {
|
|
|
|
type: DataTypes.JSON,
|
|
|
|
allowNull: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
indexes: [
|
|
|
|
{
|
2017-11-14 10:31:26 -06:00
|
|
|
fields: [ 'state', 'category' ]
|
2017-05-02 15:02:27 -05:00
|
|
|
}
|
2017-05-22 13:58:25 -05:00
|
|
|
]
|
2017-05-02 15:02:27 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-11-09 10:51:58 -06:00
|
|
|
const classMethods = [ listWithLimitByCategory ]
|
2017-05-22 13:58:25 -05:00
|
|
|
addMethodsToModel(Job, classMethods)
|
|
|
|
|
2017-05-02 15:02:27 -05:00
|
|
|
return Job
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) {
|
2017-05-02 15:02:27 -05:00
|
|
|
const query = {
|
|
|
|
order: [
|
|
|
|
[ 'id', 'ASC' ]
|
|
|
|
],
|
|
|
|
limit: limit,
|
|
|
|
where: {
|
2017-11-14 10:31:26 -06:00
|
|
|
state,
|
|
|
|
category: jobCategory
|
2017-05-02 15:02:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return Job.findAll(query)
|
2017-05-02 15:02:27 -05:00
|
|
|
}
|