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