|
| 1 | +import { BadRequestException } from '@nestjs/common' |
| 2 | +import { |
| 3 | + CronConsoleHandlerArgument, |
| 4 | + getCronConsoleHandlers, |
| 5 | +} from '~/_common/decorators/cron-console-handler.decorator' |
| 6 | + |
| 7 | +function getHandlerDescriptor(handler: string) { |
| 8 | + return getCronConsoleHandlers().find((entry) => entry.handler === handler) |
| 9 | +} |
| 10 | + |
| 11 | +function assertArgumentType(argument: CronConsoleHandlerArgument, value: unknown, handler: string): void { |
| 12 | + if (value === undefined || value === null || value === '') { |
| 13 | + return |
| 14 | + } |
| 15 | + |
| 16 | + if (argument.type === 'number' && !Number.isFinite(Number(value))) { |
| 17 | + throw new BadRequestException(`L'argument "${argument.name}" doit être numérique pour le handler "${handler}".`) |
| 18 | + } |
| 19 | + |
| 20 | + if (argument.type === 'boolean' && typeof value !== 'boolean') { |
| 21 | + throw new BadRequestException(`L'argument "${argument.name}" doit être un booléen pour le handler "${handler}".`) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +export function validateCronTaskOptions( |
| 26 | + handler: string, |
| 27 | + options?: string[] | Record<string, unknown>, |
| 28 | +): void { |
| 29 | + if (options === undefined || options === null) { |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + const descriptor = getHandlerDescriptor(handler) |
| 34 | + const schema = descriptor?.arguments || [] |
| 35 | + |
| 36 | + if (Array.isArray(options)) { |
| 37 | + if (schema.length > 0) { |
| 38 | + throw new BadRequestException( |
| 39 | + `Le handler "${handler}" attend un objet d'arguments (${schema.map((argument) => argument.name).join(', ')}).`, |
| 40 | + ) |
| 41 | + } |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + if (typeof options !== 'object') { |
| 46 | + throw new BadRequestException(`Les options du handler "${handler}" doivent être un objet clé-valeur.`) |
| 47 | + } |
| 48 | + |
| 49 | + if (!schema.length) { |
| 50 | + if (Object.keys(options).length > 0) { |
| 51 | + throw new BadRequestException(`Le handler "${handler}" n'accepte pas d'arguments configurables.`) |
| 52 | + } |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + const allowed = new Map(schema.map((argument) => [argument.name, argument])) |
| 57 | + |
| 58 | + for (const key of Object.keys(options)) { |
| 59 | + if (!allowed.has(key)) { |
| 60 | + throw new BadRequestException(`Argument "${key}" invalide pour le handler "${handler}".`) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + for (const argument of schema) { |
| 65 | + const value = options[argument.name] |
| 66 | + if (argument.required && (value === undefined || value === null || value === '')) { |
| 67 | + throw new BadRequestException(`L'argument "${argument.name}" est requis pour le handler "${handler}".`) |
| 68 | + } |
| 69 | + assertArgumentType(argument, value, handler) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export function buildCronCommandArgs( |
| 74 | + handler: string, |
| 75 | + options?: string[] | Record<string, unknown> | null, |
| 76 | +): { positionalArgs: string[]; flagArgs: string[] } { |
| 77 | + const descriptor = getHandlerDescriptor(handler) |
| 78 | + const schema = descriptor?.arguments || [] |
| 79 | + const positionalArgs: string[] = [] |
| 80 | + const flagArgs: string[] = [] |
| 81 | + |
| 82 | + if (!options) { |
| 83 | + return { positionalArgs, flagArgs } |
| 84 | + } |
| 85 | + |
| 86 | + if (Array.isArray(options)) { |
| 87 | + positionalArgs.push(...options.map(String).filter((value) => value.length > 0)) |
| 88 | + return { positionalArgs, flagArgs } |
| 89 | + } |
| 90 | + |
| 91 | + if (typeof options !== 'object') { |
| 92 | + return { positionalArgs, flagArgs } |
| 93 | + } |
| 94 | + |
| 95 | + const positionalSchema = schema.filter((argument) => argument.positional) |
| 96 | + const flagSchema = schema.filter((argument) => !argument.positional) |
| 97 | + |
| 98 | + for (const argument of positionalSchema) { |
| 99 | + const value = options[argument.name] |
| 100 | + if (value !== undefined && value !== null && `${value}`.length > 0) { |
| 101 | + positionalArgs.push(String(value)) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + const flagEntries = flagSchema.length |
| 106 | + ? flagSchema.map((argument) => [argument.name, options[argument.name]] as const) |
| 107 | + : Object.entries(options).filter(([key]) => !positionalSchema.some((argument) => argument.name === key)) |
| 108 | + |
| 109 | + for (const [key, value] of flagEntries) { |
| 110 | + if (typeof value === 'boolean') { |
| 111 | + if (value) { |
| 112 | + flagArgs.push(`--${key}`) |
| 113 | + } |
| 114 | + continue |
| 115 | + } |
| 116 | + |
| 117 | + if (value === null || value === undefined || `${value}`.length === 0) { |
| 118 | + continue |
| 119 | + } |
| 120 | + |
| 121 | + if (typeof value === 'object') { |
| 122 | + flagArgs.push(`--${key}='${JSON.stringify(value)}'`) |
| 123 | + continue |
| 124 | + } |
| 125 | + |
| 126 | + flagArgs.push(`--${key}='${String(value)}'`) |
| 127 | + } |
| 128 | + |
| 129 | + return { positionalArgs, flagArgs } |
| 130 | +} |
0 commit comments