Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement get client api address and location #147

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/workflow_for_ecomm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ jobs:
API_KEY_URL: ${{ secrets.API_KEY_URL }}
SUBSCRIPTION_KEY: ${{ secrets.SUBSCRIPTION_KEY }}
DEFAULT_PROFILE_URL: ${{ secrets.DEFAULT_PROFILE_URL }}
IP_KEYS: ${{ secrets.IP_KEYS }}
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/__test__/Momo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ describe('Buyer Controller Tests', () => {
const orderRepository = dbConnection.getRepository(Order);
order = orderRepository.create({
totalAmount: 100,
country: 'RW',
status: 'Pending',
trackingNumber: '123456',
paid: false,
});

orderAmount = orderRepository.create({
totalAmount: 0,
country: 'RW',
status: 'Pending',
trackingNumber: '123456',
paid: false,
Expand Down
56 changes: 41 additions & 15 deletions src/__test__/category.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import request from 'supertest';
import app from '../app';
import { afterAllHook, beforeAllHook, getAdminToken, getVendorToken } from './testSetup';
import {
afterAllHook,
beforeAllHook,
getAdminToken,
getVendorToken,
} from './testSetup';
import { Order } from '../database/models/orderEntity';
import dbConnection from '../database';

beforeAll(beforeAllHook);
afterAll(afterAllHook);

describe('Category Creation Tests', () => {
beforeAll(async () => {
token = await getVendorToken();
adminToken = await getAdminToken()
adminToken = await getAdminToken();
});
let token: string;
let categoryId: number;
Expand All @@ -18,7 +25,7 @@ describe('Category Creation Tests', () => {
const categoryData = {
name: 'Test Category',
description: 'Test category description',
icon: 'Test category icon'
icon: 'Test category icon',
};

const response = await request(app)
Expand All @@ -39,7 +46,7 @@ describe('Category Creation Tests', () => {
it('should return a 400 status code if name is missing', async () => {
const invalidData = {
description: 'Test category description',
icon: 'Test category icon'
icon: 'Test category icon',
};

const response = await request(app)
Expand All @@ -54,7 +61,7 @@ describe('Category Creation Tests', () => {
it('should return a 400 status code if icon is missing', async () => {
const invalidData = {
description: 'Test category description',
name: 'Test category name'
name: 'Test category name',
};

const response = await request(app)
Expand Down Expand Up @@ -82,7 +89,7 @@ describe('Category Creation Tests', () => {
const existingCategoryData = {
name: 'Existing Category',
description: 'Existing category description',
icon: 'Existing category icon'
icon: 'Existing category icon',
};
await request(app)
.post('/api/v1/category')
Expand All @@ -92,7 +99,7 @@ describe('Category Creation Tests', () => {
const newCategoryData = {
name: 'Existing Category',
description: 'Existing category description',
icon: 'Existing category icon'
icon: 'Existing category icon',
};
const response = await request(app)
.post('/api/v1/category')
Expand Down Expand Up @@ -133,7 +140,7 @@ describe('Category Creation Tests', () => {
const updatedCategoryData = {
name: 'Updated Category Name',
description: 'Updated category description',
icon: 'Updated category icon'
icon: 'Updated category icon',
};

const response = await request(app)
Expand All @@ -153,7 +160,7 @@ describe('Category Creation Tests', () => {
const existingCategoryData = {
name: 'Existing Category',
description: 'Existing category description',
icon: 'Existing category icon'
icon: 'Existing category icon',
};
await request(app)
.post('/api/v1/category')
Expand All @@ -163,7 +170,7 @@ describe('Category Creation Tests', () => {
const updateCategoryData = {
name: 'Existing Category',
description: 'Existing category description',
icon: 'Existing category icon'
icon: 'Existing category icon',
};
const response = await request(app)
.put(`/api/v1/category/${categoryId}`)
Expand All @@ -181,7 +188,7 @@ describe('Category Creation Tests', () => {
.send({
name: 'Updated Category Name',
description: 'Updated category description',
icon: 'Updated category icon'
icon: 'Updated category icon',
});

expect(response.status).toBe(404);
Expand Down Expand Up @@ -209,9 +216,28 @@ describe('Category Creation Tests', () => {
it('should return an array of category metrics', async () => {
const response = await request(app)
.get('/api/v1/category/get_metrics')
.set('Authorization', `Bearer ${adminToken}`)
.set('Authorization', `Bearer ${adminToken}`);

expect(response.status).toBe(200)
expect(response.body.data).toBeDefined()
})
expect(response.status).toBe(200);
expect(response.body.data).toBeDefined();
});
it('should return an array of sales by counrty', async () => {
// Create a mock order in the database
const orderRepository = dbConnection.getRepository(Order);
const order = orderRepository.create({
totalAmount: 100,
country: 'RW',
status: 'Pending',
trackingNumber: '123456',
paid: false,
});
await orderRepository.save(order);

const response = await request(app)
.get('/api/v1/category/getSalesByCountry')
.set('Authorization', `Bearer ${adminToken}`);

expect(response.status).toBe(200);
expect(response.body.counter).toBeDefined();
});
});
2 changes: 2 additions & 0 deletions src/__test__/orderController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('Order Routes', () => {
it('should update order status to Failed', async () => {
const order = orderRepository.create({
status: 'Pending',
country: 'RW',
totalAmount: 40,
trackingNumber: '34343653',
});
Expand All @@ -40,6 +41,7 @@ describe('Order Routes', () => {
const order = orderRepository.create({
status: 'Pending',
totalAmount: 40,
country: 'RW',
trackingNumber: '34343653',
});
await orderRepository.save(order);
Expand Down
1 change: 1 addition & 0 deletions src/__test__/payment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('handlePayment', () => {
order = orderRepository.create({
totalAmount: 100,
status: 'Pending',
country: 'RW',
trackingNumber: '123456',
paid: false,
});
Expand Down
38 changes: 23 additions & 15 deletions src/controller/cartController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import applyCoupon from '../utilis/couponCalculator';
import { Order } from '../database/models/orderEntity';
import { OrderDetails } from '../database/models/orderDetailsEntity';
import { check, validationResult } from 'express-validator';


import {eventEmitter} from '../Notification.vendor/event.services'
import { eventEmitter } from '../Notification.vendor/event.services';
import axios from 'axios';

const cartRepository = dbConnection.getRepository(Cart);
const productRepository = dbConnection.getRepository(Product);
Expand Down Expand Up @@ -65,9 +64,9 @@ export const addToCart = errorHandler(async (req: Request, res: Response) => {
newItem.quantity = quantity;

const savedItem = await cartRepository.save(newItem);
eventEmitter.emit('addToCart',productId, userId)

eventEmitter.emit('addToCart', productId, userId);

return res
.status(201)
.json({ msg: 'Item added to cart successfully', cartItem: savedItem });
Expand Down Expand Up @@ -150,7 +149,7 @@ export const removeItem = errorHandler(async (req: Request, res: Response) => {

const cartItem = await cartRepository.findOne({
where: { id: itemId },
select:{user:{id:true}, product:{id:true}},
select: { user: { id: true }, product: { id: true } },
relations: ['user', 'product'],
});

Expand All @@ -159,7 +158,7 @@ export const removeItem = errorHandler(async (req: Request, res: Response) => {
}
const deletedItem = await cartRepository.delete(itemId);

eventEmitter.emit('removeItem', cartItem)
eventEmitter.emit('removeItem', cartItem);

return res.status(200).json({
msg: 'Cart Item deleted successfully',
Expand Down Expand Up @@ -198,6 +197,17 @@ export const checkout = [
// Fetch the user who is checking out
const user = await userRepository.findOne({ where: { id: userId } });

// Get user country location
let country: string = '';
try {
const response = await axios.get(
`https://ipgeolocation.abstractapi.com/v1/?api_key=${process.env.IP_KEYS}&ip_address=`
);
country = response.data.country_code;
} catch (error) {
res.send(error);
}

// Fetch the cart items for this user
const cartItems = await cartRepository.find({
where: { user: { id: userId } },
Expand Down Expand Up @@ -233,7 +243,6 @@ export const checkout = [
orderDetail.price = price;

orderDetails.push(orderDetail);

}

// Ensure totalAmount is an integer
Expand All @@ -246,12 +255,13 @@ export const checkout = [
order.totalAmount = totalAmount;
order.status = 'Pending';
order.deliveryInfo = deliveryInfo;
order.country = country;
order.trackingNumber = trackingNumber;
order.orderDetails = orderDetails;

const savedOrder = await orderRepository.save(order);
eventEmitter.emit('pressorder', order)

eventEmitter.emit('pressorder', order);

await cartRepository.delete({ user: { id: userId } });

Expand Down Expand Up @@ -292,11 +302,9 @@ export const cancelOrder = errorHandler(async (req: Request, res: Response) => {
if (!order) {
return res.status(404).json({ msg: 'Order not found' });
}
eventEmitter.emit('order_canceled', orderId)

await orderRepository.remove(order);
eventEmitter.emit('order_canceled', orderId);


await orderRepository.remove(order);

return res.status(200).json({ msg: 'Order canceled successfully' });
});
Loading
Loading