feat(Accounts): setthing up account services and e2e testing of the routes

This commit is contained in:
Matthieu Haineault 2026-02-06 08:37:40 -05:00
parent 6d17baf6c8
commit 6d911014de
10 changed files with 176 additions and 68 deletions

View File

@ -14,9 +14,11 @@ import { IdentityAndAccountModule } from 'src/identity-and-account/identity-and-
import { ChatbotModule } from 'src/chatbot/chatbot.module';
import { PrismaMariadbModule } from 'prisma/mariadb/prisma-mariadb.module';
import { PrismaLegacyModule } from 'prisma/prisma-legacy/prisma-legacy.module';
import { AccountModule } from 'src/customer-support/accounts/account.module';
@Module({
imports: [
AccountModule,
AuthenticationModule,
ConfigModule.forRoot({ isGlobal: true }),
ScheduleModule.forRoot(), //cronjobs

View File

@ -0,0 +1,6 @@
# GET http://localhost:3000/accounts
# GET http://localhost:3000/accounts/6912
GET http://localhost:3000/accounts/account/6912

View File

@ -1,19 +1,28 @@
import { Controller, Get, Param } from "@nestjs/common";
import { AccountService } from "src/customer-support/accounts/account.service";
import { AccountMemoService } from "src/customer-support/accounts/services/account-memo.service";
import { AccountService } from "src/customer-support/accounts/services/account.service";
@Controller()
@Controller('accounts')
export class AccountController {
constructor(private readonly accountService: AccountService) { }
constructor(
private readonly accountService: AccountService,
private readonly memoService: AccountMemoService,
) { }
@Get()
findAllAccounts(){
return this.accountService.findAllAccounts();
}
@Get()
@Get('account/:id')
findAccountById(@Param('accountId') accountId: number){
return this.accountService.findAccountById(accountId);
}
@Get(':id')
findMemosByAccountId(@Param('accountId') accountId: number) {
return this.accountService.findMemosByAccountId(accountId);
return this.memoService.findMemosByAccountId(accountId);
}
}

View File

@ -1,13 +1,10 @@
import { Module } from "@nestjs/common";
import { PrismaMariaDbService } from "prisma/mariadb/prisma-mariadb.service";
import { AccountController } from "src/customer-support/accounts/account.controller";
import { AccountService } from "src/customer-support/accounts/account.service";
import { AccountService } from "src/customer-support/accounts/services/account.service";
@Module({
providers: [
AccountService
],
controllers: [
AccountController
],
controllers: [ AccountController ],
providers: [ AccountService, PrismaMariaDbService ],
}) export class AccountModule { };

View File

@ -0,0 +1,6 @@
import { Injectable } from "@nestjs/common";
@Injectable()
export class AccountCreateService {
}

View File

@ -0,0 +1,6 @@
import { Injectable } from "@nestjs/common";
@Injectable()
export class AccountMemoUpdateService {
}

View File

@ -0,0 +1,29 @@
import { Injectable } from "@nestjs/common";
import { PrismaMariaDbService } from "prisma/mariadb/prisma-mariadb.service";
import { Result } from "src/common/errors/result-error.factory";
import { AccountMemo } from "src/customer-support/accounts/account.dto";
@Injectable()
export class AccountMemoService {
constructor(private readonly prismaMariaDb: PrismaMariaDbService) { }
findMemosByAccountId = async (accountId: number): Promise<Result<AccountMemo[], string>> => {
const listOfMemos: AccountMemo[] = [];
const rawListOfMemos = await this.prismaMariaDb.account_memo.findMany({
where: { id: accountId },
select: { last_updated: true, staff_id: true, memo: true },
});
if (!rawListOfMemos) return { success: false, error: 'MEMOS_NOT_FOUND' };
for (const memo of rawListOfMemos) {
listOfMemos.push({
last_updated: Number(memo.last_updated),
staff_id: Number(memo.staff_id),
memo: memo.memo ? memo.memo : '',
});
}
return { success: true, data: listOfMemos }
}
}

View File

@ -0,0 +1,56 @@
// import { Injectable } from "@nestjs/common";
// import { PrismaMariaDbService } from "prisma/mariadb/prisma-mariadb.service";
// import { Result } from "src/common/errors/result-error.factory";
// import { Account } from "src/customer-support/accounts/account.dto";
// @Injectable()
// export class AccountUpdateService {
// constructor(private readonly prisma: PrismaMariaDbService) { }
// async updateAccount(account: Account): Promise<Result<boolean, string>> {
// const oldAccountInfos = await this.prisma.account.findUnique({
// where: { id: account.id },
// });
// if (!oldAccountInfos) return { success: false, error: 'ACCOUNT_NOT_FOUND' };
// await this.prisma.account.update({
// where: { id: oldAccountInfos.id },
// data: {
// customer_id: account.customerId,
// language_id: account.language,
// username: account.username,
// password: account.password,
// group_id: account.groupId,
// status: account.status,
// first_name: account.firstName,
// last_name: account.lastName,
// mandataire: account.mandataire,
// title: account.title,
// email: account.email,
// company: account.company,
// contact: account.contact,
// address1: account.address,
// address2: account.address,
// tel_home: account.telHome,
// tel_office: account.telOffice,
// tel_office_ext: account.telOffice_ext,
// cell: account.cell,
// fax: account.fax,
// land_owner: account.landOwner,
// commercial: account.commercial,
// vip: account.vip,
// notes_client: account.notes_client,
// terminate_reason: account.terminateReason,
// terminate_cie: account.terminateCie,
// terminate_date: account.terminateDate,
// terminate_note: account.terminateNote,
// mauvais_payeur: account.mauvaisPayeur,
// },
// });
// }
// }

View File

@ -1,12 +1,12 @@
import { Injectable } from "@nestjs/common";
import { PrismaClient as MariadbClient } from "prisma/mariadb/generated/prisma/client/mariadb/client";
import { PrismaMariaDbService } from "prisma/mariadb/prisma-mariadb.service";
import { Result } from "src/common/errors/result-error.factory";
import { Account, AccountMemo } from "src/customer-support/accounts/account.dto";
import { Account } from "src/customer-support/accounts/account.dto";
@Injectable()
export class AccountService {
constructor(private readonly prismaMariaDb: MariadbClient) { }
constructor(private readonly prismaMariaDb: PrismaMariaDbService) { }
findAllAccounts = async (): Promise<Result<Account[], string>> => {
const listOfAccounts: Account[] = [];
@ -70,22 +70,59 @@ export class AccountService {
}
}
findMemosByAccountId = async (accountId: number): Promise<Result<AccountMemo[], string>> => {
const listOfMemos: AccountMemo[] = [];
findAccountById = async (accountId: number): Promise<Result<Account, string>> => {
const rawListOfMemos = await this.prismaMariaDb.account_memo.findMany({
where: { account_id: accountId },
select: { last_updated: true, staff_id: true, memo: true },
const rawAccount = await this.prismaMariaDb.account.findUnique({
where: { id: accountId }
});
if (!rawListOfMemos) return { success: false, error: 'MEMOS_NOT_FOUND' };
if (!rawAccount) return { success: false, error: 'ACCOUNT_NOT_FOUND' }
for (const memo of rawListOfMemos) {
listOfMemos.push({
last_updated: Number(memo.last_updated),
staff_id: Number(memo.staff_id),
memo: memo.memo ? memo.memo : '',
});
const emailList: string[] = [
rawAccount.email ? rawAccount.email : '',
rawAccount.email_autre ? rawAccount.email_autre : '',
];
const addressList: string[] = [
rawAccount.address1 ? rawAccount.address1 : '',
rawAccount.address2 ? rawAccount.address2 : '',
rawAccount.city ? rawAccount.city : '',
rawAccount.state ? rawAccount.state : '',
rawAccount.zip ? rawAccount.zip : '',
rawAccount.country_id.toString(),
];
const account: Account = {
id: Number(rawAccount.id),
customerId: rawAccount.customer_id ? rawAccount.customer_id : '',
language: rawAccount.language_id,
username: rawAccount.username ? rawAccount.username : '',
password: rawAccount.password ? rawAccount.password : '',
groupId: rawAccount.group_id ? rawAccount.group_id : 0,
status: rawAccount.status ? rawAccount.status : 0,
firstName: rawAccount.first_name ? rawAccount.first_name : '',
lastName: rawAccount.last_name ? rawAccount.last_name : '',
mandataire: rawAccount.mandataire ? rawAccount.mandataire : '',
title: rawAccount.title ? rawAccount.title : '',
email: emailList,
company: rawAccount.company ? rawAccount.company : '',
contact: rawAccount.contact,
address: addressList,
telHome: rawAccount.tel_home ? rawAccount.tel_home : '',
telOffice: rawAccount.tel_office ? rawAccount.tel_office : '',
telOffice_ext: rawAccount.tel_office_ext ? rawAccount.tel_office_ext : '',
cell: rawAccount.cell ? rawAccount.cell : '',
fax: rawAccount.fax ? rawAccount.fax : '',
landOwner: rawAccount.land_owner,
commercial: rawAccount.commercial,
vip: rawAccount.vip,
notes_client: rawAccount.notes_client ? rawAccount.notes_client : '',
terminateReason: rawAccount.terminate_reason ? rawAccount.terminate_reason : '',
terminateCie: rawAccount.terminate_cie ? rawAccount.terminate_cie : '',
terminateNote: rawAccount.terminate_note ? rawAccount.terminate_note : '',
terminateDate: rawAccount.terminate_date ? rawAccount.terminate_date : '',
mauvaisPayeur: rawAccount.mauvais_payeur,
}
return { success: true, data: listOfMemos }
return { success: true, data: account };
}
}

View File

@ -6,20 +6,11 @@ if (!(globalThis as any).crypto) {
import { ensureAttachmentsTmpDir } from './time-and-attendance/attachments/config/attachment.fs';
import { NestFactory, Reflector } from '@nestjs/core';
import { AppModule } from './app.module';
// import { JwtAuthGuard } from './modules/authentication/guards/jwt-auth.guard';
import { ModulesGuard } from './common/guards/modules.guard';
// import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
// import { writeFileSync } from 'fs';
import * as session from 'express-session';
import * as passport from 'passport';
import { PrismaSessionStore } from '@quixo3/prisma-session-store';
import { PrismaPostgresService } from 'prisma/postgres/prisma-postgres.service';
// import { initSupervisor } from 'scripts/init-supervisor';
// import { initializePaidTimeOff } from 'scripts/init-paid-time-off';
// import { initializePreferences } from 'scripts/init-preferences-access';
// import { extractOldTimesheets } from 'scripts/migrate-timesheets';
// import { extractOldShifts } from 'scripts/migrate-shifts';
// import { extractOldExpenses } from 'scripts/migrate-expenses';
const SESSION_TOKEN_DURATION_MINUTES = 180
@ -30,7 +21,6 @@ async function bootstrap() {
const reflector = app.get(Reflector);
app.useGlobalGuards(
// new JwtAuthGuard(reflector), //Authentification JWT
new ModulesGuard(reflector), //deny-by-default and Module-based Access Control
);
@ -60,36 +50,6 @@ async function bootstrap() {
credentials: true,
});
// //swagger config
// const config = new DocumentBuilder()
// .setTitle('Targo_Backend')
// .setDescription('Documentation de l`API REST pour Targo (NestJS + Prisma)')
// .setVersion('1.0')
// .addBearerAuth({
// type: 'http',
// scheme: 'bearer',
// bearerFormat: 'JWT',
// name: 'Authorization',
// description: 'Invalid JWT token',
// in: 'header',
// }, 'access-token')
// .addTag('Users')
// .addTag('Employees')
// .addTag('Customers')
// .addTag('Timesheets')
// .addTag('Shifts')
// .addTag('Leave Requests')
// .addTag('Shift Codes')
// .addTag('OAuth Access Tokens')
// .addTag('Authorization')
// .build();
// //document builder for swagger docs
// const documentFactory = () => SwaggerModule.createDocument(app, config);
// const document = documentFactory()
// SwaggerModule.setup('api/docs', app, document);
// writeFileSync('./docs/swagger/swagger-spec.json', JSON.stringify(document, null, 2));
await ensureAttachmentsTmpDir();
await app.listen(process.env.PORT ?? 3000);