// import { PrismaClient as PrismaNew } from "@prisma/client"; // import { PrismaClient as PrismaLegacy } from "@prisma/client-legacy" // const prisma_legacy = new PrismaLegacy({}); // const prisma = new PrismaNew({}); // type NewEmployee = { // id: number; // company_code: number; // external_payroll_id: number; // } // type OldShifts = { // time_sheet_id: string | null; // code: string | null; // type: string | null; // date: Date | null; // start_time: bigint | null; // end_time: bigint | null; // comment: string | null; // status: boolean | null; // } // export const extractOldShifts = async () => { // for (let id = 1; id <= 61; id++) { // console.log(`Start of shift migration ***************************************************************`); // const new_employee = await findOneNewEmployee(id); // console.log(`Employee ${id} found in new DB`); // const new_timesheets = await findManyNewTimesheets(new_employee.id); // console.log(`New Timesheets found for employee ${id}`); // for (const ts of new_timesheets) { // console.log(`start_date = ${ts.start_date} timesheet_id = ${ts.id}`) // } // console.log('***************************************************************'); // const old_employee_id = await findOneOldEmployee(new_employee); // console.log(`Employee ${new_employee.id} found in old DB`); // const old_timesheets = await findManyOldTimesheets(old_employee_id); // console.log(`Timesheets for employee ${old_employee_id}/${new_employee.id} found in old DB`); // for (const old_timesheet of old_timesheets) { // if (!old_timesheet.start_date) continue; // const new_timesheet = new_timesheets.find((ts) => ts.start_date.getTime() === old_timesheet.start_date!.getTime()); // if (!new_timesheet) { // console.warn(`No new timesheet ${new_timesheet} matching legacy timesheet ${old_timesheet.id}`); // continue; // } // const old_shifts = await prisma_legacy.shifts.findMany({ // where: { time_sheet_id: old_timesheet.id }, // select: { // time_sheet_id: true, // code: true, // type: true, // date: true, // start_time: true, // end_time: true, // comment: true, // status: true, // }, // }); // await createManyNewShifts(new_timesheet.id, old_shifts); // } // } // await prisma_legacy.$disconnect(); // await prisma.$disconnect(); // } // const findOneNewEmployee = async (id: number): Promise => { // const new_employee = await prisma.employees.findUnique({ // where: { id: id }, // select: { // id: true, // company_code: true, // external_payroll_id: true, // }, // }); // if (!new_employee) throw new Error(`New Employee with id ${id} not found`) // return new_employee; // } // const findOneOldEmployee = async (new_employee: NewEmployee): Promise => { // const old_employee = await prisma_legacy.employees.findFirst({ // where: { // company: new_employee.company_code, // employee_number: new_employee.external_payroll_id.toString(), // }, // select: { // id: true, // }, // }); // if (!old_employee) throw new Error(`Old Employee not found`); // return old_employee.id; // } // const findManyOldTimesheets = async (old_employee_id: string) => { // const old_timesheets = await prisma_legacy.time_sheets.findMany({ // where: { employee_id: old_employee_id }, // select: { id: true, start_date: true, status: true } // }); // return old_timesheets; // } // const findManyNewTimesheets = async (employee_id: number) => { // const timesheets = await prisma.timesheets.findMany({ // where: { employee_id: employee_id }, // select: { id: true, start_date: true } // }) // return timesheets; // } // const createManyNewShifts = async (timesheet_id: number, old_shifts: OldShifts[]) => { // for (const old_shift of old_shifts) { // let is_remote = true; // const start = toHHmmfromLegacyTimestamp(old_shift.start_time); // if (old_shift.start_time == null || !start) { // console.warn(`Shift start invalid ${old_shift.start_time}`); // continue; // } // const end = toHHmmfromLegacyTimestamp(old_shift.end_time); // if (old_shift.end_time == null || !end) { // console.warn(`Shift end invalid ${old_shift.end_time}`); // continue; // } // if (old_shift.date == null) { // console.warn(`Shift date invalid ${old_shift.date}`); // continue; // } // if (old_shift.status == null) { // console.warn(`status null for legacy shift ${old_shift}`); // continue; // } // if (old_shift.type == null) { // console.warn(`type null for legacy shift ${old_shift.type}`); // continue; // } // if (old_shift.type === 'office') { // is_remote = false; // } // if (old_shift.code == null) { // console.warn(`Code null for legacy shift ${old_shift.code}`); // continue; // } // const bank_code_id = await findBankCodeIdUsingOldCode(old_shift.code); // try { // await prisma.shifts.create({ // // where: { unique_ts_id_date_start_time: { // // timesheet_id, // // date: old_shift.date, // // start_time: toDateFromHHmm(start) }}, // // update: { // // start_time: toDateFromHHmm(start), // // end_time: toDateFromHHmm(end), // // comment: old_shift.comment, // // is_approved: old_shift.status, // // is_remote: is_remote, // // bank_code_id: bank_code_id, // // }, // data: { // date: old_shift.date, // start_time: toDateFromHHmm(start), // end_time: toDateFromHHmm(end), // comment: old_shift.comment, // is_approved: old_shift.status, // is_remote: is_remote, // timesheet_id: timesheet_id, // bank_code_id: bank_code_id, // }, // }); // } catch (error) { // console.log('An error occured during shifts creation'); // } // } // } // const toHHmmfromLegacyTimestamp = (value: bigint | null): string | null => { // if (value == null) return null; // const date = new Date(Number(value)); // const hh = String(date.getHours()).padStart(2, '0'); // const mm = String(date.getMinutes()).padStart(2, '0'); // return `${hh}:${mm}`; // } // const toDateFromHHmm = (hhmm: string): Date => { // const [hh, mm] = hhmm.split(':'); // const hours = Number(hh); // const minutes = Number(mm); // return new Date(Date.UTC(1970, 0, 1, hours, minutes, 0, 0)); // } // const findBankCodeIdUsingOldCode = async (code: string): Promise => { // if (code === 'G700') { // code = 'G104'; // } else if (code === 'G140') { // code = 'G56' // } // const bank_code = await prisma.bankCodes.findFirst({ // where: { bank_code: code }, // select: { id: true, bank_code: true }, // }); // if (!bank_code) throw new Error(`Bank_code_id not found for Code ${code}`) // return bank_code.id; // }