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": {
"operationId": "ExpenseController_create",
"parameters": [
{
"name": "timesheet_id",
"required": true,
"in": "path",
"schema": {
"type": "number"
}
}
],
"parameters": [],
"requestBody": {
"required": true,
"content": {
@ -638,7 +629,7 @@
]
}
},
"/expense": {
"/expense/update": {
"patch": {
"operationId": "ExpenseController_update",
"parameters": [],
@ -652,7 +643,7 @@
]
}
},
"/expense/{expense_id}": {
"/expense/delete/{expense_id}": {
"delete": {
"operationId": "ExpenseController_remove",
"parameters": [

View File

@ -12,7 +12,8 @@ export class AuthController {
@Get('/callback')
@UseGuards(OIDCLoginGuard)
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')

View File

@ -46,7 +46,7 @@ async function bootstrap() {
// Enable CORS
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,
});

View File

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

View File

@ -15,7 +15,7 @@ export class ExpenseUpsertService {
//_________________________________________________________________
// CREATE
//_________________________________________________________________
async createExpense(timesheet_id: number, dto: ExpenseDto): Promise<CreateExpenseResult> {
async createExpense( dto: ExpenseDto): Promise<CreateExpenseResult> {
try {
//normalize strings and dates
const normed_expense = this.normalizeExpenseDto(dto);
@ -25,10 +25,15 @@ export class ExpenseUpsertService {
const parsed_mileage = this.parseOptionalNumber(dto.mileage, "mileage");
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
const expense = await this.prisma.expenses.create({
data: {
timesheet_id,
timesheet_id: timesheet.id,
bank_code_id: dto.bank_code_id,
attachment: parsed_attachment,
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({
where: { employee_id, start_date: start_date },
where: { employee_id, start_date: start },
include: {
employee: { include: { user: true } },
shift: { include: { bank_code: true } },
@ -193,13 +194,13 @@ export class GetTimesheetsOverviewService {
await this.prisma.timesheets.create({
data: {
employee_id,
start_date: start_date,
start_date: start,
is_approved: false
},
});
row = await this.prisma.timesheets.findFirst({
where: { employee_id, start_date: start_date },
where: { employee_id, start_date: start },
include: {
employee: { include: { user: true } },
shift: { include: { bank_code: true } },

View File

@ -40,7 +40,7 @@ export const toHHmmFromDate = (input: Date | string): string => {
//converts Date format to string
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) => {