|
| 1 | +import { MutationError, ErrorCodeEnum } from './../helpers/MutationError'; |
| 2 | +import { findQueue } from '../helpers'; |
| 3 | +import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose'; |
| 4 | +import { getJobTC } from '../types/job/Job'; |
| 5 | +import { Options } from '../definitions'; |
| 6 | + |
| 7 | +export function createJobMoveToDelayedFC( |
| 8 | + sc: SchemaComposer<any>, |
| 9 | + opts: Options |
| 10 | +): ObjectTypeComposerFieldConfigAsObjectDefinition<any, any> { |
| 11 | + const { typePrefix } = opts; |
| 12 | + |
| 13 | + return { |
| 14 | + description: 'Moves job from active to delayed.', |
| 15 | + type: sc.createObjectTC({ |
| 16 | + name: `${typePrefix}JobMoveToDelayedPayload`, |
| 17 | + fields: { |
| 18 | + job: getJobTC(sc, opts), |
| 19 | + willBeProcessedOn: 'Date', |
| 20 | + }, |
| 21 | + }), |
| 22 | + args: { |
| 23 | + prefix: { |
| 24 | + type: 'String!', |
| 25 | + defaultValue: 'bull', |
| 26 | + }, |
| 27 | + queueName: 'String!', |
| 28 | + id: 'String!', |
| 29 | + delay: { |
| 30 | + type: 'Int!', |
| 31 | + defaultValue: 60000, |
| 32 | + }, |
| 33 | + }, |
| 34 | + resolve: async (_, { prefix, queueName, id, delay }) => { |
| 35 | + const queue = await findQueue(prefix, queueName, opts); |
| 36 | + const job = await queue.getJob(id); |
| 37 | + if (!job) throw new MutationError('Job not found!', ErrorCodeEnum.JOB_NOT_FOUND); |
| 38 | + const willBeProcessedOn = Date.now() + delay; |
| 39 | + await job.moveToDelayed(willBeProcessedOn); |
| 40 | + return { |
| 41 | + willBeProcessedOn, |
| 42 | + job, |
| 43 | + }; |
| 44 | + }, |
| 45 | + }; |
| 46 | +} |
0 commit comments