From 5cd80545422bba855cc9a730a2e13cc9d982c34b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 30 Nov 2017 10:51:13 +0100 Subject: Add ability to list jobs --- server/models/job/job.ts | 52 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 12 deletions(-) (limited to 'server/models/job/job.ts') diff --git a/server/models/job/job.ts b/server/models/job/job.ts index c2d088090..f428e26db 100644 --- a/server/models/job/job.ts +++ b/server/models/job/job.ts @@ -1,19 +1,14 @@ import { values } from 'lodash' import * as Sequelize from 'sequelize' - -import { JOB_STATES, JOB_CATEGORIES } from '../../initializers' - -import { addMethodsToModel } from '../utils' -import { - JobInstance, - JobAttributes, - - JobMethods -} from './job-interface' import { JobCategory, JobState } from '../../../shared/models/job.model' +import { JOB_CATEGORIES, JOB_STATES } from '../../initializers' +import { addMethodsToModel, getSort } from '../utils' +import { JobAttributes, JobInstance, JobMethods } from './job-interface' let Job: Sequelize.Model let listWithLimitByCategory: JobMethods.ListWithLimitByCategory +let listForApi: JobMethods.ListForApi +let toFormattedJSON: JobMethods.ToFormattedJSON export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { Job = sequelize.define('Job', @@ -44,12 +39,30 @@ export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Se } ) - const classMethods = [ listWithLimitByCategory ] - addMethodsToModel(Job, classMethods) + const classMethods = [ + listWithLimitByCategory, + listForApi + ] + const instanceMethods = [ + toFormattedJSON + ] + addMethodsToModel(Job, classMethods, instanceMethods) return Job } +toFormattedJSON = function (this: JobInstance) { + return { + id: this.id, + state: this.state, + category: this.category, + handlerName: this.handlerName, + handlerInputData: this.handlerInputData, + createdAt: this.createdAt, + updatedAt: this.updatedAt + } +} + // --------------------------------------------------------------------------- listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) { @@ -66,3 +79,18 @@ listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: return Job.findAll(query) } + +listForApi = function (start: number, count: number, sort: string) { + const query = { + offset: start, + limit: count, + order: [ getSort(sort) ] + } + + return Job.findAndCountAll(query).then(({ rows, count }) => { + return { + data: rows, + total: count + } + }) +} -- cgit v1.2.3