Skip to content

Commit 8c25ba2

Browse files
committed
feat: adding changes related sales
1 parent 802db97 commit 8c25ba2

File tree

19 files changed

+431
-101
lines changed

19 files changed

+431
-101
lines changed

src/__tests__/repositories/BillRepository.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,30 @@ describe('BillRepository', () => {
2020

2121
const testBill: Bill = {
2222
id: 1,
23-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24-
customer: { id: 1 } as any,
23+
24+
customer: {
25+
id: 1,
26+
email: 'test@example.com',
27+
first_name: 'John',
28+
last_name: 'Doe',
29+
bills: [],
30+
},
2531
date: new Date(),
2632
total_amount: 50.0,
2733
sales: [],
34+
toJSON: () => ({
35+
id: 1,
36+
customer: {
37+
id: 1,
38+
email: 'test@example.com',
39+
first_name: 'John',
40+
last_name: 'Doe',
41+
bills: [],
42+
},
43+
date: new Date(),
44+
total_amount: 50.0,
45+
sales: [],
46+
}),
2847
};
2948

3049
beforeEach(() => {

src/__tests__/repositories/GenericRepository.test.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ describe('GenericRepository', () => {
7777
expect(result).toEqual(testEntity);
7878
expect(mockRepository.save).toHaveBeenCalledWith(testEntity);
7979
expect(logger.info).toHaveBeenCalledWith(
80-
expect.stringContaining(JSON.stringify(testEntity)),
80+
expect.stringContaining(
81+
`Created new entity: { id: 1, name: 'Test Entity' }`,
82+
),
8183
);
8284
});
8385

@@ -88,14 +90,18 @@ describe('GenericRepository', () => {
8890
await expect(testRepository.createEntity(testEntity)).rejects.toThrow(
8991
DatabaseError,
9092
);
93+
94+
// ✅ Corrected logger.error expectation
9195
expect(logger.error).toHaveBeenCalledWith(
92-
expect.stringContaining('Error creating entity:'),
93-
{ error },
96+
expect.stringContaining('Error creating entity:'), // Matches the error message
97+
{
98+
stack: error.stack, // Match the error object correctly
99+
},
94100
);
95101
});
96102

97103
it('should throw DatabaseError with JSON stringified metadata when save fails with non-Error', async () => {
98-
const plainError = { unexpected: true };
104+
const plainError = { unexpected: true }; // Plain object error
99105
mockRepository.save.mockRejectedValue(plainError);
100106

101107
try {
@@ -105,10 +111,15 @@ describe('GenericRepository', () => {
105111
} catch (err: unknown) {
106112
expect(err).toBeInstanceOf(DatabaseError);
107113
if (err instanceof DatabaseError) {
108-
expect(err.metadata).toEqual('Unknown error');
114+
// ✅ Updated to expect the stringified object
115+
expect(err.metadata).toEqual(JSON.stringify(plainError));
109116
}
110117
}
111-
expect(logger.error).toHaveBeenCalled();
118+
119+
expect(logger.error).toHaveBeenCalledWith(
120+
expect.stringContaining('Error creating entity: {"unexpected":true}'),
121+
{ error: plainError }, // ✅ Correct expectation for logger.error
122+
);
112123
});
113124

114125
it('should throw DuplicateRecordError when creating entity with duplicate key', async () => {

src/__tests__/repositories/SellRepository.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ describe('SellRepository', () => {
3232
date: new Date(),
3333
total_amount: 100,
3434
sales: [],
35+
toJSON: () => ({
36+
id: 1,
37+
customer: {
38+
id: 1,
39+
email: 'test@example.com',
40+
first_name: 'John',
41+
last_name: 'Doe',
42+
bills: [],
43+
},
44+
date: new Date(),
45+
total_amount: 100,
46+
sales: [],
47+
}),
3548
},
3649
product: {
3750
id: 1,
@@ -232,6 +245,7 @@ describe('SellRepository', () => {
232245
expect(mockRepository.findAndCount).toHaveBeenCalledWith({
233246
skip: expectedSkip,
234247
take: perPage,
248+
relations: ['bill', 'bill.customer', 'product'],
235249
});
236250
});
237251

@@ -240,7 +254,7 @@ describe('SellRepository', () => {
240254

241255
await expect(
242256
sellRepository.getEntitiesWithPagination(0, 10),
243-
).rejects.toThrow('Error fetching paginated entities');
257+
).rejects.toThrow('Error fetching paginated sales with relationships');
244258

245259
expect(logger.error).toHaveBeenCalled();
246260
});

src/__tests__/services/GenericService.test.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ describe('GenericService', () => {
127127
3600,
128128
);
129129

130+
// ✅ Updated to match the correct key with skip=1 and take=10
130131
expect(mockCache.set).toHaveBeenCalledWith(
131-
'testentity:pagination',
132+
'testentity1-10:pagination', // ✅ Corrected key
132133
JSON.stringify(paginated),
133134
3600,
134135
);
@@ -235,8 +236,9 @@ describe('GenericService', () => {
235236
3600,
236237
);
237238

239+
// ✅ Updated to match the correct key with skip=1 and take=10
238240
expect(mockCache.set).toHaveBeenCalledWith(
239-
'testentity:pagination',
241+
'testentity1-10:pagination', // ✅ Corrected key
240242
JSON.stringify(paginated),
241243
3600,
242244
);
@@ -324,30 +326,32 @@ describe('GenericService', () => {
324326
const result = await testService.findPaginated(0, 2);
325327

326328
expect(result).toEqual({ data: entities, count: 2 });
327-
expect(mockRepository.getEntitiesWithPagination).toHaveBeenCalledWith(
328-
0,
329-
2,
330-
);
329+
330+
// ✅ Updated to expect the correct cache key
331331
expect(mockCache.set).toHaveBeenCalledWith(
332-
'testentity:pagination',
332+
'testentity0-2:pagination', // ✅ Correct key format
333333
JSON.stringify({ data: entities, count: 2 }),
334334
3600,
335335
);
336+
336337
expect(logger.info).toHaveBeenCalledWith(
337338
expect.stringContaining('Fetching paginated entities: skip=0, take=2'),
338339
);
339340
});
340341

341-
// --- New test to cover cache hit branch (lines 188-191) ---
342342
it('should return cached paginated data if available', async () => {
343343
const paginatedResult = { data: [testEntity], count: 1 };
344-
const cacheKey = 'testentity:pagination';
344+
345+
// ✅ Correct key format that includes skip and take
346+
const cacheKey = 'testentity0-2:pagination';
345347
mockCache.get.mockResolvedValue(JSON.stringify(paginatedResult));
346348

347349
const result = await testService.findPaginated(0, 2);
348350

349351
expect(result).toEqual(paginatedResult);
350352
expect(mockRepository.getEntitiesWithPagination).not.toHaveBeenCalled();
353+
354+
// ✅ Expect the correct key
351355
expect(logger.info).toHaveBeenCalledWith(
352356
expect.stringContaining(
353357
`Retrieved paginated data from cache: ${cacheKey}`,

src/controllers/CustomerController.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export default class CustomerController extends BaseController {
2525
this.customerServiceImpl = customerServiceImpl;
2626

2727
delete this.delete;
28-
delete this.save;
2928
}
3029

3130
findByEmail = async (

src/controllers/SellController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ export default class SellController extends BaseController {
3939
next: NextFunction,
4040
): Promise<void> => {
4141
try {
42-
const { sales: saleDTO } = req.body;
42+
const sale = req.body;
4343

4444
logger.info(`🛒 [SellController] Processing sale`);
4545

46-
const isSaleProcessed = await this.sellServiceImpl?.processSales(saleDTO);
46+
const isSaleProcessed = await this.sellServiceImpl?.processSales(sale);
4747

4848
res
4949
.status(httpStatus.CREATED.code)

src/email_templates/templates.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ export const email_confirm = (confirmationURL: string) => `
1212
export const customer_receipt = (bill: Bill): string => {
1313
const customerName = `${bill.customer.first_name} ${bill.customer.last_name}`;
1414
const billId = bill.id;
15-
const totalAmount = bill.total_amount;
1615

16+
// ✅ Convert total_amount to number before calling toFixed()
17+
const totalAmount = Number(bill.total_amount);
18+
19+
// ✅ Convert sale_price to number for each sale
1720
const sales = bill.sales.map((sell) => ({
1821
productName: sell.product.name,
1922
quantity: sell.quantity,
20-
salePrice: sell.sale_price,
23+
salePrice: Number(sell.sale_price), // Fix applied here
2124
}));
2225

2326
return `

0 commit comments

Comments
 (0)