fix(expenses): fixes to prisma create request
This commit is contained in:
parent
fc88b3a032
commit
7863a15562
|
|
@ -1,14 +1,13 @@
|
|||
import { weekStartSunday, toStringFromDate, toDateFromString } from "src/common/utils/date-utils";
|
||||
import { EmailToIdResolver } from "src/common/mappers/email-id.mapper";
|
||||
import { expense_select } from "src/time-and-attendance/utils/selects.utils";
|
||||
import { PrismaService } from "src/prisma/prisma.service";
|
||||
import { ExpenseEntity } from "src/time-and-attendance/expenses/dtos/expense-entity.dto";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { Result } from "src/common/errors/result-error.factory";
|
||||
import { NormalizedExpense } from "src/time-and-attendance/utils/type.utils";
|
||||
import { weekStartSunday, toStringFromDate, toDateFromString } from "src/common/utils/date-utils";
|
||||
import { BankCodesResolver } from "src/common/mappers/bank-type-id.mapper";
|
||||
import { EmployeeTimesheetResolver } from "src/common/mappers/timesheet.mapper";
|
||||
import { ExpenseDto } from "src/time-and-attendance/expenses/dtos/expense-create.dto";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
|
||||
@Injectable()
|
||||
|
|
@ -17,7 +16,6 @@ export class ExpenseUpsertService {
|
|||
private readonly prisma: PrismaService,
|
||||
private readonly emailResolver: EmailToIdResolver,
|
||||
private readonly typeResolver: BankCodesResolver,
|
||||
private readonly timesheetResolver: EmployeeTimesheetResolver,
|
||||
) { }
|
||||
|
||||
//_________________________________________________________________
|
||||
|
|
@ -45,9 +43,6 @@ export class ExpenseUpsertService {
|
|||
const expense = await this.prisma.expenses.create({
|
||||
data: {
|
||||
...normed_expense.data,
|
||||
amount: normed_expense.data.parsed_amount,
|
||||
mileage: normed_expense.data.parsed_mileage,
|
||||
attachment: normed_expense.data.parsed_attachment,
|
||||
timesheet_id: timesheet.id,
|
||||
is_approved: dto.is_approved,
|
||||
},
|
||||
|
|
@ -89,10 +84,8 @@ export class ExpenseUpsertService {
|
|||
if (!timesheet) return { success: false, error: `Timesheet ${dto.timesheet_id} not found` }
|
||||
|
||||
//checks for modifications
|
||||
const data: ExpenseEntity = {
|
||||
const data: Prisma.ExpensesUpdateInput = {
|
||||
...normed_expense.data,
|
||||
id: dto.id,
|
||||
timesheet_id: timesheet?.id,
|
||||
is_approved: dto.is_approved,
|
||||
};
|
||||
if (!data) return { success: false, error: `An error occured during normalization. Expense with id: ${dto.id} is invalid` }
|
||||
|
|
@ -103,7 +96,7 @@ export class ExpenseUpsertService {
|
|||
data,
|
||||
select: expense_select,
|
||||
});
|
||||
if (!expense) return { success: false, error: `An error occured during update. Expense with id: ${data.id} was not updated` }
|
||||
if (!expense) return { success: false, error: `An error occured during update. Expense with id: ${dto.id} was not updated` }
|
||||
|
||||
//build an object to return to the frontend
|
||||
const updated: ExpenseDto = {
|
||||
|
|
@ -152,9 +145,9 @@ export class ExpenseUpsertService {
|
|||
//_________________________________________________________________
|
||||
//makes sure that comments are the right length the date is of Date type
|
||||
private normalizeAndParseExpenseDto = async (dto: ExpenseDto): Promise<Result<NormalizedExpense, string>> => {
|
||||
const parsed_attachment = this.parseOptionalNumber(dto.attachment, "attachment");
|
||||
const parsed_mileage = this.parseOptionalNumber(dto.mileage, "mileage");
|
||||
const parsed_amount = this.parseOptionalNumber(dto.amount, "amount");
|
||||
const attachment = this.parseOptionalNumber(dto.attachment, "attachment");
|
||||
const mileage = this.parseOptionalNumber(dto.mileage, "mileage");
|
||||
const amount = this.parseOptionalNumber(dto.amount, "amount");
|
||||
|
||||
const comment = this.truncate280(dto.comment);
|
||||
const supervisor_comment = dto.supervisor_comment && dto.supervisor_comment.trim()
|
||||
|
|
@ -170,9 +163,9 @@ export class ExpenseUpsertService {
|
|||
date,
|
||||
comment,
|
||||
supervisor_comment,
|
||||
parsed_amount,
|
||||
parsed_attachment,
|
||||
parsed_mileage,
|
||||
amount,
|
||||
attachment,
|
||||
mileage,
|
||||
bank_code_id: type.data,
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@ export type NormalizedExpense = {
|
|||
date: Date;
|
||||
comment: string;
|
||||
supervisor_comment?: string;
|
||||
parsed_amount?: number | Prisma.Decimal | null;
|
||||
parsed_mileage?: number | Prisma.Decimal | null;
|
||||
parsed_attachment?: number;
|
||||
amount?: number | Prisma.Decimal | null;
|
||||
mileage?: number | Prisma.Decimal | null;
|
||||
attachment?: number;
|
||||
bank_code_id: number;
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user