targo-backend/test/factories/employee.factory.ts

47 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// test/factories/employee.factory.ts
export type EmployeePayload = {
// user_id?: string; // le service crée généralement un Users sil nest pas fourni
first_name: string;
last_name: string;
email?: string;
phone_number: number;
residence?: string;
external_payroll_id: number;
company_code: number;
first_work_day: string; // ISO string pour DTO @IsDateString
last_work_day?: string;
};
const randInt = (min: number, max: number) =>
Math.floor(Math.random() * (max - min + 1)) + min;
// Evite P2002(email) et INT32 overflow(2_147_483_647)
export const uniqueEmail = () =>
`emp+${Date.now()}_${Math.random().toString(36).slice(2,8)}@test.local`;
export const safePhone = () =>
randInt(100_000_000, 999_999_999); // 9 chiffres < INT32
const uniqueExtPayroll = () => randInt(1_000_000, 9_999_999);
const uniqueCompanyCode = () => randInt(100_000, 999_999);
const iso = (y: number, m: number, d: number) =>
new Date(Date.UTC(y, m - 1, d)).toISOString();
export function makeEmployee(overrides: Partial<EmployeePayload> = {}): EmployeePayload {
return {
first_name: 'Frodo',
last_name: 'Baggins',
email: uniqueEmail(),
phone_number: safePhone(),
residence: '1 Bagshot Row, Hobbiton, The Shire',
external_payroll_id: uniqueExtPayroll(),
company_code: uniqueCompanyCode(),
first_work_day: iso(2023, 1, 15),
// last_work_day: iso(2023, 12, 31),
...overrides,
};
}
// volontairement invalide pour déclencher 400 via ValidationPipe
export function makeInvalidEmployee(): Record<string, unknown> {
return { first_name: '', external_payroll_id: 'nope' };
}