refactor(expenses): modified createExpense signature. removed timesheet_id from the param. ajusted auth logic

This commit is contained in:
Matthieu Haineault 2025-10-31 12:11:55 -04:00
parent 9f0da467ae
commit e0cefc8ec9
7 changed files with 26 additions and 30 deletions

View File

@ -605,19 +605,10 @@
] ]
} }
}, },
"/expense/{timesheet_id}": { "/expense/create": {
"post": { "post": {
"operationId": "ExpenseController_create", "operationId": "ExpenseController_create",
"parameters": [ "parameters": [],
{
"name": "timesheet_id",
"required": true,
"in": "path",
"schema": {
"type": "number"
}
}
],
"requestBody": { "requestBody": {
"required": true, "required": true,
"content": { "content": {
@ -638,7 +629,7 @@
] ]
} }
}, },
"/expense": { "/expense/update": {
"patch": { "patch": {
"operationId": "ExpenseController_update", "operationId": "ExpenseController_update",
"parameters": [], "parameters": [],
@ -652,7 +643,7 @@
] ]
} }
}, },
"/expense/{expense_id}": { "/expense/delete/{expense_id}": {
"delete": { "delete": {
"operationId": "ExpenseController_remove", "operationId": "ExpenseController_remove",
"parameters": [ "parameters": [

View File

@ -12,7 +12,8 @@ export class AuthController {
@Get('/callback') @Get('/callback')
@UseGuards(OIDCLoginGuard) @UseGuards(OIDCLoginGuard)
loginCallback(@Req() req: Request, @Res() res: Response) { loginCallback(@Req() req: Request, @Res() res: Response) {
res.redirect('http://10.100.251.2:9011/#/login-success'); // res.redirect('http://10.100.251.2:9011/#/login-success');
res.redirect('http://localhost:9000/#/login-success');
} }
@Get('/me') @Get('/me')

View File

@ -46,7 +46,7 @@ async function bootstrap() {
// Enable CORS // Enable CORS
app.enableCors({ app.enableCors({
origin: ['http://10.100.251.2:9011', 'http://10.100.251.2:9012', 'http://10.100.251.2:9013'], origin: ['http://10.100.251.2:9011', 'http://10.100.251.2:9012', 'http://10.100.251.2:9013', 'http://localhost:9000'],
credentials: true, credentials: true,
}); });

View File

@ -9,20 +9,18 @@ import { ExpenseDto } from "src/time-and-attendance/modules/expenses/dtos/expens
export class ExpenseController { export class ExpenseController {
constructor( private readonly upsert_service: ExpenseUpsertService ){} constructor( private readonly upsert_service: ExpenseUpsertService ){}
@Post(':timesheet_id') @Post('create')
create( create(@Body() dto: ExpenseDto): Promise<CreateExpenseResult>{
@Param('timesheet_id', ParseIntPipe) timesheet_id: number, return this.upsert_service.createExpense(dto);
@Body() dto: ExpenseDto): Promise<CreateExpenseResult>{
return this.upsert_service.createExpense(timesheet_id, dto);
} }
@Patch() @Patch('update')
update( update(
@Body() body: { update :{ id: number; dto: updateExpenseDto }}): Promise<UpdateExpenseResult>{ @Body() body: { update :{ id: number; dto: updateExpenseDto }}): Promise<UpdateExpenseResult>{
return this.upsert_service.updateExpense(body.update); return this.upsert_service.updateExpense(body.update);
} }
@Delete(':expense_id') @Delete('delete/:expense_id')
remove(@Param('expense_id') expense_id: number) { remove(@Param('expense_id') expense_id: number) {
return this.upsert_service.deleteExpense(expense_id); return this.upsert_service.deleteExpense(expense_id);
} }

View File

@ -15,7 +15,7 @@ export class ExpenseUpsertService {
//_________________________________________________________________ //_________________________________________________________________
// CREATE // CREATE
//_________________________________________________________________ //_________________________________________________________________
async createExpense(timesheet_id: number, dto: ExpenseDto): Promise<CreateExpenseResult> { async createExpense( dto: ExpenseDto): Promise<CreateExpenseResult> {
try { try {
//normalize strings and dates //normalize strings and dates
const normed_expense = this.normalizeExpenseDto(dto); const normed_expense = this.normalizeExpenseDto(dto);
@ -25,10 +25,15 @@ export class ExpenseUpsertService {
const parsed_mileage = this.parseOptionalNumber(dto.mileage, "mileage"); const parsed_mileage = this.parseOptionalNumber(dto.mileage, "mileage");
const parsed_attachment = this.parseOptionalNumber(dto.attachment, "attachment"); const parsed_attachment = this.parseOptionalNumber(dto.attachment, "attachment");
const timesheet = await this.prisma.timesheets.findUnique({
where: { id: dto.timesheet_id },
select: { id: true },
});
if(!timesheet) throw new NotFoundException(`Timesheet with id ${dto.timesheet_id} not found`);
//create a new expense //create a new expense
const expense = await this.prisma.expenses.create({ const expense = await this.prisma.expenses.create({
data: { data: {
timesheet_id, timesheet_id: timesheet.id,
bank_code_id: dto.bank_code_id, bank_code_id: dto.bank_code_id,
attachment: parsed_attachment, attachment: parsed_attachment,
date: normed_expense.date, date: normed_expense.date,

View File

@ -178,10 +178,11 @@ export class GetTimesheetsOverviewService {
}; };
} }
private ensureTimesheet = async (employee_id: number, start_date: Date) => { private ensureTimesheet = async (employee_id: number, start_date: Date | string) => {
const start = toDateFromString(start_date);
let row = await this.prisma.timesheets.findFirst({ let row = await this.prisma.timesheets.findFirst({
where: { employee_id, start_date: start_date }, where: { employee_id, start_date: start },
include: { include: {
employee: { include: { user: true } }, employee: { include: { user: true } },
shift: { include: { bank_code: true } }, shift: { include: { bank_code: true } },
@ -193,13 +194,13 @@ export class GetTimesheetsOverviewService {
await this.prisma.timesheets.create({ await this.prisma.timesheets.create({
data: { data: {
employee_id, employee_id,
start_date: start_date, start_date: start,
is_approved: false is_approved: false
}, },
}); });
row = await this.prisma.timesheets.findFirst({ row = await this.prisma.timesheets.findFirst({
where: { employee_id, start_date: start_date }, where: { employee_id, start_date: start },
include: { include: {
employee: { include: { user: true } }, employee: { include: { user: true } },
shift: { include: { bank_code: true } }, shift: { include: { bank_code: true } },

View File

@ -40,7 +40,7 @@ export const toHHmmFromDate = (input: Date | string): string => {
//converts Date format to string //converts Date format to string
export const toDateFromString = (ymd: string | Date): Date => { export const toDateFromString = (ymd: string | Date): Date => {
return new Date(`${ymd}T00:00:00:000Z`); return new Date(`${ymd}`);
} }
export const toUTCDateFromString = (iso: string | Date) => { export const toUTCDateFromString = (iso: string | Date) => {