diff options
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/account/user-interface.ts | 8 | ||||
-rw-r--r-- | server/models/job/job-interface.ts | 15 | ||||
-rw-r--r-- | server/models/job/job.ts | 52 |
3 files changed, 54 insertions, 21 deletions
diff --git a/server/models/account/user-interface.ts b/server/models/account/user-interface.ts index 1a04fb750..0f0b72063 100644 --- a/server/models/account/user-interface.ts +++ b/server/models/account/user-interface.ts | |||
@@ -1,12 +1,10 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
3 | 2 | import * as Sequelize from 'sequelize' | |
4 | // Don't use barrel, import just what we need | ||
5 | import { AccountInstance } from './account-interface' | ||
6 | import { User as FormattedUser } from '../../../shared/models/users/user.model' | ||
7 | import { ResultList } from '../../../shared/models/result-list.model' | 3 | import { ResultList } from '../../../shared/models/result-list.model' |
8 | import { UserRight } from '../../../shared/models/users/user-right.enum' | 4 | import { UserRight } from '../../../shared/models/users/user-right.enum' |
9 | import { UserRole } from '../../../shared/models/users/user-role' | 5 | import { UserRole } from '../../../shared/models/users/user-role' |
6 | import { User as FormattedUser } from '../../../shared/models/users/user.model' | ||
7 | import { AccountInstance } from './account-interface' | ||
10 | 8 | ||
11 | export namespace UserMethods { | 9 | export namespace UserMethods { |
12 | export type HasRight = (this: UserInstance, right: UserRight) => boolean | 10 | export type HasRight = (this: UserInstance, right: UserRight) => boolean |
diff --git a/server/models/job/job-interface.ts b/server/models/job/job-interface.ts index 411a05029..3cfc0fbed 100644 --- a/server/models/job/job-interface.ts +++ b/server/models/job/job-interface.ts | |||
@@ -1,18 +1,23 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
1 | import * as Sequelize from 'sequelize' | 2 | import * as Sequelize from 'sequelize' |
2 | import * as Promise from 'bluebird' | 3 | import { Job as FormattedJob, JobCategory, JobState } from '../../../shared/models/job.model' |
3 | 4 | import { ResultList } from '../../../shared/models/result-list.model' | |
4 | import { JobCategory, JobState } from '../../../shared/models/job.model' | ||
5 | 5 | ||
6 | export namespace JobMethods { | 6 | export namespace JobMethods { |
7 | export type ListWithLimitByCategory = (limit: number, state: JobState, category: JobCategory) => Promise<JobInstance[]> | 7 | export type ListWithLimitByCategory = (limit: number, state: JobState, category: JobCategory) => Bluebird<JobInstance[]> |
8 | export type ListForApi = (start: number, count: number, sort: string) => Bluebird< ResultList<JobInstance> > | ||
9 | |||
10 | export type ToFormattedJSON = (this: JobInstance) => FormattedJob | ||
8 | } | 11 | } |
9 | 12 | ||
10 | export interface JobClass { | 13 | export interface JobClass { |
11 | listWithLimitByCategory: JobMethods.ListWithLimitByCategory | 14 | listWithLimitByCategory: JobMethods.ListWithLimitByCategory |
15 | listForApi: JobMethods.ListForApi, | ||
12 | } | 16 | } |
13 | 17 | ||
14 | export interface JobAttributes { | 18 | export interface JobAttributes { |
15 | state: JobState | 19 | state: JobState |
20 | category: JobCategory | ||
16 | handlerName: string | 21 | handlerName: string |
17 | handlerInputData: any | 22 | handlerInputData: any |
18 | } | 23 | } |
@@ -21,6 +26,8 @@ export interface JobInstance extends JobClass, JobAttributes, Sequelize.Instance | |||
21 | id: number | 26 | id: number |
22 | createdAt: Date | 27 | createdAt: Date |
23 | updatedAt: Date | 28 | updatedAt: Date |
29 | |||
30 | toFormattedJSON: JobMethods.ToFormattedJSON | ||
24 | } | 31 | } |
25 | 32 | ||
26 | export interface JobModel extends JobClass, Sequelize.Model<JobInstance, JobAttributes> {} | 33 | export interface JobModel extends JobClass, Sequelize.Model<JobInstance, JobAttributes> {} |
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 @@ | |||
1 | import { values } from 'lodash' | 1 | import { values } from 'lodash' |
2 | import * as Sequelize from 'sequelize' | 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' | 3 | import { JobCategory, JobState } from '../../../shared/models/job.model' |
4 | import { JOB_CATEGORIES, JOB_STATES } from '../../initializers' | ||
5 | import { addMethodsToModel, getSort } from '../utils' | ||
6 | import { JobAttributes, JobInstance, JobMethods } from './job-interface' | ||
14 | 7 | ||
15 | let Job: Sequelize.Model<JobInstance, JobAttributes> | 8 | let Job: Sequelize.Model<JobInstance, JobAttributes> |
16 | let listWithLimitByCategory: JobMethods.ListWithLimitByCategory | 9 | let listWithLimitByCategory: JobMethods.ListWithLimitByCategory |
10 | let listForApi: JobMethods.ListForApi | ||
11 | let toFormattedJSON: JobMethods.ToFormattedJSON | ||
17 | 12 | ||
18 | export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | 13 | export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
19 | Job = sequelize.define<JobInstance, JobAttributes>('Job', | 14 | Job = sequelize.define<JobInstance, JobAttributes>('Job', |
@@ -44,12 +39,30 @@ export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Se | |||
44 | } | 39 | } |
45 | ) | 40 | ) |
46 | 41 | ||
47 | const classMethods = [ listWithLimitByCategory ] | 42 | const classMethods = [ |
48 | addMethodsToModel(Job, classMethods) | 43 | listWithLimitByCategory, |
44 | listForApi | ||
45 | ] | ||
46 | const instanceMethods = [ | ||
47 | toFormattedJSON | ||
48 | ] | ||
49 | addMethodsToModel(Job, classMethods, instanceMethods) | ||
49 | 50 | ||
50 | return Job | 51 | return Job |
51 | } | 52 | } |
52 | 53 | ||
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 | |||
53 | // --------------------------------------------------------------------------- | 66 | // --------------------------------------------------------------------------- |
54 | 67 | ||
55 | listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) { | 68 | listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) { |
@@ -66,3 +79,18 @@ listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: | |||
66 | 79 | ||
67 | return Job.findAll(query) | 80 | return Job.findAll(query) |
68 | } | 81 | } |
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 | } | ||