Skip to content

Commit

Permalink
Merge branch 'ft-stripe-payment' of https://github.com/atlp-rwanda/kn…
Browse files Browse the repository at this point in the history
…ights-ecomm-be into ft-stripe-payment
  • Loading branch information
maxCastro1 committed May 30, 2024
2 parents 2e40bfc + 00261d8 commit 52094a8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
verbose: true,
forceExit: true,
clearMocks: true,
testTimeout: 30000,
testTimeout: 50000,
resetMocks: true,
restoreMocks: true,
collectCoverageFrom: [
Expand Down
2 changes: 1 addition & 1 deletion src/__test__/cart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,4 +608,4 @@ describe('Order management tests', () => {
expect(response.status).toBe(500);
});
});
});
});
3 changes: 3 additions & 0 deletions src/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export class User {
@UpdateDateColumn()
updatedAt!: Date;

@Column({ type: 'numeric', precision: 24, scale: 2, default: 0 })
accountBalance!: number;

@BeforeInsert()
setRole (): void {
this.role = this.userType === 'Vendor' ? roles.vendor : roles.buyer;
Expand Down
12 changes: 11 additions & 1 deletion src/entities/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export class Transaction {
@IsNumber()
amount!: number;

@Column({ type: 'numeric', precision: 15, scale: 2, default: 0 })
@IsNotEmpty()
@IsNumber()
previousBalance!: number;

@Column({ type: 'numeric', precision: 15, scale: 2, default: 0 })
@IsNotEmpty()
@IsNumber()
currentBalance!: number;

@Column({ type: 'enum', enum: ['debit', 'credit'] })
@IsNotEmpty()
@IsString()
Expand All @@ -48,4 +58,4 @@ export class Transaction {

@UpdateDateColumn()
updatedAt!: Date;
}
}
12 changes: 11 additions & 1 deletion src/services/orderServices/createOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ export const createOrderService = async (req: Request, res: Response) => {
orderItems.push(orderItem);
}

if (!buyer.accountBalance || buyer.accountBalance < totalPrice) {
return sendErrorResponse(res, 400, 'Not enough funds to perform this transaction');
}

const previousBalance = buyer.accountBalance;
buyer.accountBalance -= totalPrice;
const currentBalance = buyer.accountBalance;

const newOrder = new Order();
newOrder.buyer = buyer;
newOrder.totalPrice = totalPrice;
Expand All @@ -86,6 +94,8 @@ export const createOrderService = async (req: Request, res: Response) => {
orderTransaction.user = buyer;
orderTransaction.order = newOrder;
orderTransaction.amount = totalPrice;
orderTransaction.previousBalance = previousBalance;
orderTransaction.currentBalance = currentBalance;
orderTransaction.type = 'debit';
orderTransaction.description = 'Purchase of products';
await transactionalEntityManager.save(Transaction, orderTransaction);
Expand Down Expand Up @@ -173,4 +183,4 @@ const saveVendorRelatedOrder = async (order: Order, CartItem: CartItem[]) => {
} catch (error) {
console.log((error as Error).message);
}
};
};

0 comments on commit 52094a8

Please sign in to comment.