fix(mariaDB): fixed account service functions to use new prisma setup
This commit is contained in:
parent
8b9c2f7a86
commit
789e9acf89
|
|
@ -1,12 +1,12 @@
|
|||
import { Injectable } from "@nestjs/common";
|
||||
import { PrismaClient } from "prisma/generated/mariadb/client";
|
||||
import { PrismaClient as MariaDbPrismaClient } from "prisma/generated/mariadb/client";
|
||||
import { Result } from "src/common/errors/result-error.factory";
|
||||
import { Account, AccountMemo } from "src/customer-support/accounts/account.dto";
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class AccountService {
|
||||
constructor(private readonly prismaMariaDb: PrismaClient) { }
|
||||
constructor(private readonly prismaMariaDb: MariaDbPrismaClient) { }
|
||||
|
||||
findAllAccounts = async (): Promise<Result<Account[], string>> => {
|
||||
const listOfAccounts: Account[] = [];
|
||||
|
|
@ -89,65 +89,3 @@ export class AccountService {
|
|||
return { success: true, data: listOfMemos }
|
||||
}
|
||||
}
|
||||
|
||||
export const findAllAccounts = async (prismaMariaDb: PrismaClient): Promise<Result<Account[], string>> => {
|
||||
const listOfAccounts: Account[] = [];
|
||||
|
||||
const rawListOfAccounts = await prismaMariaDb.account.findMany({});
|
||||
if (!rawListOfAccounts) return { success: false, error: 'ACCOUNTS_NOT_FOUND' }
|
||||
try {
|
||||
|
||||
for (const account of rawListOfAccounts) {
|
||||
const emailList: string[] = [
|
||||
account.email ? account.email : '',
|
||||
account.email_autre ? account.email_autre : '',
|
||||
];
|
||||
|
||||
const addressList: string[] = [
|
||||
account.address1 ? account.address1 : '',
|
||||
account.address2 ? account.address2 : '',
|
||||
account.city ? account.city : '',
|
||||
account.state ? account.state : '',
|
||||
account.zip ? account.zip : '',
|
||||
account.country_id.toString(),
|
||||
];
|
||||
|
||||
listOfAccounts.push({
|
||||
id: Number(account.id),
|
||||
customerId: account.customer_id ? account.customer_id : '',
|
||||
language: account.language_id,
|
||||
username: account.username ? account.username : '',
|
||||
password: account.password ? account.password : '',
|
||||
groupId: account.group_id ? account.group_id : 0,
|
||||
status: account.status ? account.status : 0,
|
||||
firstName: account.first_name ? account.first_name : '',
|
||||
lastName: account.last_name ? account.last_name : '',
|
||||
mandataire: account.mandataire ? account.mandataire : '',
|
||||
title: account.title ? account.title : '',
|
||||
email: emailList,
|
||||
company: account.company ? account.company : '',
|
||||
contact: account.contact,
|
||||
address: addressList,
|
||||
telHome: account.tel_home ? account.tel_home : '',
|
||||
telOffice: account.tel_office ? account.tel_office : '',
|
||||
telOffice_ext: account.tel_office_ext ? account.tel_office_ext : '',
|
||||
cell: account.cell ? account.cell : '',
|
||||
fax: account.fax ? account.fax : '',
|
||||
landOwner: account.land_owner,
|
||||
commercial: account.commercial,
|
||||
vip: account.vip,
|
||||
notes_client: account.notes_client ? account.notes_client : '',
|
||||
terminateReason: account.terminate_reason ? account.terminate_reason : '',
|
||||
terminateCie: account.terminate_cie ? account.terminate_cie : '',
|
||||
terminateNote: account.terminate_note ? account.terminate_note : '',
|
||||
terminateDate: account.terminate_date ? account.terminate_date : '',
|
||||
mauvaisPayeur: account.mauvais_payeur,
|
||||
});
|
||||
}
|
||||
console.log(listOfAccounts);
|
||||
return { success: true, data: listOfAccounts };
|
||||
|
||||
} catch (error) {
|
||||
return { success: false, error: error + " \n An error occured during retrieving the list of accounts" }
|
||||
}
|
||||
}
|
||||
58
src/customer-support/accounts/test-account.service.ts
Normal file
58
src/customer-support/accounts/test-account.service.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#!/usr/bin/env ts-node
|
||||
|
||||
import { adapterMariaDb } from 'prisma.config.mariadb';
|
||||
import { PrismaClient as MariaDbPrismaClient } from 'prisma/generated/mariadb/client';
|
||||
import { AccountService } from 'src/customer-support/accounts/account.service';
|
||||
|
||||
async function main() {
|
||||
// Initialize the Prisma client
|
||||
const prismaMariaDb = new MariaDbPrismaClient({ adapter: adapterMariaDb });
|
||||
|
||||
// Initialize the service
|
||||
const accountService = new AccountService(prismaMariaDb);
|
||||
|
||||
try {
|
||||
// Test 1: Find all accounts
|
||||
console.log('=== Testing findAllAccounts ===');
|
||||
const accountsResult = await accountService.findAllAccounts();
|
||||
|
||||
if (accountsResult.success) {
|
||||
console.log(`✓ Successfully retrieved ${accountsResult.data.length} accounts`);
|
||||
console.log('First account:', JSON.stringify(accountsResult.data[0], null, 2));
|
||||
} else {
|
||||
console.error('✗ Error:', accountsResult.error);
|
||||
}
|
||||
|
||||
// Test 2: Find memos by account ID
|
||||
console.log('\n=== Testing findMemosByAccountId ===');
|
||||
// Replace with an actual account ID from your database
|
||||
const testAccountId = 1;
|
||||
const memosResult = await accountService.findMemosByAccountId(testAccountId);
|
||||
|
||||
if (memosResult.success) {
|
||||
console.log(`✓ Successfully retrieved ${memosResult.data.length} memos for account ${testAccountId}`);
|
||||
if (memosResult.data.length > 0) {
|
||||
console.log('First memo:', JSON.stringify(memosResult.data[0], null, 2));
|
||||
}
|
||||
} else {
|
||||
console.error('✗ Error:', memosResult.error);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Unexpected error:', error);
|
||||
} finally {
|
||||
// Clean up: disconnect from database
|
||||
await prismaMariaDb.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
// Run the main function
|
||||
main()
|
||||
.then(() => {
|
||||
console.log('\n✓ Test completed');
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('\n✗ Test failed:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user