aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/models/job/job.ts
blob: ba1c6737ee4b888cc103ae8030e70df9babc2e06 (plain) (tree)
1
2
3
4
5
6
7
8
9
                               

                                                                                                      
                                                               
                                  
 


                   
     
                                     
     
   




                                               
 


                                                
 


                     
 


                        
 
            
                 

            
                 









                                                                                             

                    
     

                                  

   





                                                                  
 





                                                                      

   
                      
            






                                              
     
   
 
import { values } from 'lodash'
import { AllowNull, Column, CreatedAt, DataType, Model, Table, UpdatedAt } from 'sequelize-typescript'
import { JobCategory, JobState } from '../../../shared/models'
import { JOB_CATEGORIES, JOB_STATES } from '../../initializers'
import { getSort } from '../utils'

@Table({
  tableName: 'job',
  indexes: [
    {
      fields: [ 'state', 'category' ]
    }
  ]
})
export class JobModel extends Model<JobModel> {
  @AllowNull(false)
  @Column(DataType.ENUM(values(JOB_STATES)))
  state: JobState

  @AllowNull(false)
  @Column(DataType.ENUM(values(JOB_CATEGORIES)))
  category: JobCategory

  @AllowNull(false)
  @Column
  handlerName: string

  @AllowNull(true)
  @Column(DataType.JSON)
  handlerInputData: any

  @CreatedAt
  createdAt: Date

  @UpdatedAt
  updatedAt: Date

  static listWithLimitByCategory (limit: number, state: JobState, jobCategory: JobCategory) {
    const query = {
      order: [
        [ 'id', 'ASC' ]
      ],
      limit: limit,
      where: {
        state,
        category: jobCategory
      },
      logging: false
    }

    return JobModel.findAll(query)
  }

  static listForApi (start: number, count: number, sort: string) {
    const query = {
      offset: start,
      limit: count,
      order: [ getSort(sort) ]
    }

    return JobModel.findAndCountAll(query).then(({ rows, count }) => {
      return {
        data: rows,
        total: count
      }
    })
  }

  toFormattedJSON () {
    return {
      id: this.id,
      state: this.state,
      category: this.category,
      handlerName: this.handlerName,
      handlerInputData: this.handlerInputData,
      createdAt: this.createdAt,
      updatedAt: this.updatedAt
    }
  }
}