144 lines
4.6 KiB
TypeScript
144 lines
4.6 KiB
TypeScript
import { Injectable, NotFoundException } from "@nestjs/common";
|
|
import { PrismaService } from "src/prisma/prisma.service";
|
|
import { CreateShiftDto } from "../dtos/create-shifts.dto";
|
|
import { Shifts, ShiftsArchive } from "@prisma/client";
|
|
import { UpdateShiftsDto } from "../dtos/update-shifts.dto";
|
|
|
|
@Injectable()
|
|
export class ShiftsService {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
async create(dto: CreateShiftDto): Promise<Shifts> {
|
|
const { timesheet_id, bank_code_id, date, start_time, end_time } = dto;
|
|
return this.prisma.shifts.create({
|
|
data: { timesheet_id, bank_code_id, date, start_time, end_time },
|
|
include: {
|
|
timesheet: {
|
|
include: {
|
|
employee: { include: { user: true } },
|
|
},
|
|
},
|
|
bank_code: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
findAll(): Promise<Shifts[]> {
|
|
return this.prisma.shifts.findMany({
|
|
include: {
|
|
timesheet: {
|
|
include: {
|
|
employee: {
|
|
include: { user:true }
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async findOne(id: number): Promise<Shifts> {
|
|
const shift = await this.prisma.shifts.findUnique({
|
|
where: { id },
|
|
include: {
|
|
timesheet: {
|
|
include: {
|
|
employee: {
|
|
include: { user: true }
|
|
},
|
|
},
|
|
},
|
|
bank_code: true,
|
|
},
|
|
});
|
|
if(!shift) {
|
|
throw new NotFoundException(`Shift #${id} not found`);
|
|
}
|
|
return shift;
|
|
}
|
|
|
|
async update(id: number, dto: UpdateShiftsDto): Promise<Shifts> {
|
|
await this.findOne(id);
|
|
const { timesheet_id, bank_code_id, date,start_time,end_time} = dto;
|
|
return this.prisma.shifts.update({
|
|
where: { id },
|
|
data: {
|
|
...(timesheet_id !== undefined && { timesheet_id }),
|
|
...(bank_code_id !== undefined && { bank_code_id }),
|
|
...(date !== undefined && { date }),
|
|
...(start_time !== undefined && { start_time }),
|
|
...(end_time !== undefined && { end_time }),
|
|
},
|
|
include: {
|
|
timesheet: {
|
|
include: {
|
|
employee: {
|
|
include: { user: true }
|
|
},
|
|
},
|
|
},
|
|
bank_code: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async remove(id: number): Promise<Shifts> {
|
|
await this.findOne(id);
|
|
return this.prisma.shifts.delete({ where: { id } });
|
|
}
|
|
|
|
//archivation functions ******************************************************
|
|
|
|
async archiveOld(): Promise<void> {
|
|
//fetches archived timesheet's Ids
|
|
const archivedTimesheets = await this.prisma.timesheetsArchive.findMany({
|
|
select: { timesheet_id: true },
|
|
});
|
|
|
|
const timesheetIds = archivedTimesheets.map(sheet => sheet.timesheet_id);
|
|
if(timesheetIds.length === 0) {
|
|
return;
|
|
}
|
|
|
|
// copy/delete transaction
|
|
await this.prisma.$transaction(async transaction => {
|
|
//fetches shifts to move to archive
|
|
const shiftsToArchive = await transaction.shifts.findMany({
|
|
where: { timesheet_id: { in: timesheetIds } },
|
|
});
|
|
if(shiftsToArchive.length === 0) {
|
|
return;
|
|
}
|
|
|
|
//copies sent to archive table
|
|
await transaction.shiftsArchive.createMany({
|
|
data: shiftsToArchive.map(shift => ({
|
|
shift_id: shift.id,
|
|
timesheet_id: shift.timesheet_id,
|
|
bank_code_id: shift.bank_code_id,
|
|
description: shift.description ?? undefined,
|
|
date: shift.date,
|
|
start_time: shift.start_time,
|
|
end_time: shift.end_time,
|
|
})),
|
|
});
|
|
|
|
//delete from shifts table
|
|
await transaction.shifts.deleteMany({
|
|
where: { id: { in: shiftsToArchive.map(shift => shift.id) } },
|
|
})
|
|
|
|
})
|
|
}
|
|
|
|
//fetches all archived timesheets
|
|
async findAllArchived(): Promise<ShiftsArchive[]> {
|
|
return this.prisma.shiftsArchive.findMany();
|
|
}
|
|
|
|
//fetches an archived timesheet
|
|
async findOneArchived(id: number): Promise<ShiftsArchive> {
|
|
return this.prisma.shiftsArchive.findUniqueOrThrow({ where: { id } });
|
|
}
|
|
|
|
} |