Skip to content

Commit

Permalink
get country_code instead of country_name
Browse files Browse the repository at this point in the history
  • Loading branch information
wayneleon1 committed Jul 12, 2024
1 parent 3f5840c commit a619c5c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/controller/cartController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,8 @@ export const checkout = [
const response = await axios.get(
`https://ipgeolocation.abstractapi.com/v1/?api_key=${process.env.IP_KEYS}&ip_address=`
);
country = response.data.country;
country = response.data.country_code;
} catch (error) {
console.error(error);
res.send(error);
}

Expand Down
40 changes: 40 additions & 0 deletions src/controller/categoryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,43 @@ export const getCategoryMetrics = errorHandler(
return res.status(200).json({ data: data.slice(0, 4) });
}
);

// Function Get Sales by Country
export const getSalesByCountry = errorHandler(
async (req: Request, res: Response) => {
const orders = await orderRepository.find({
where: {
paid: false,
},
select: {
id: true,
totalAmount: true,
country: true,
paid: true,
orderDetails: {
id: true,
price: true,
quantity: true,
product: {
id: true,
name: true,
},
},
},
relations: ['orderDetails', 'orderDetails.product'],
});

const counter: { [key: string]: number } = {};
for (const order of orders) {
for (const orderDetail of order.orderDetails) {
if (order.country in counter) {
counter[order.country] += orderDetail.price;
} else {
counter[order.country] = orderDetail.price;
}
}
}

return res.status(200).json({ counter });
}
);
5 changes: 4 additions & 1 deletion src/controller/productController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,6 @@ export const checkProductAvailability = async (req: Request, res: Response) => {
};

// Best seller API endpoints

export const getBestSellingProducts = async (req: Request, res: Response) => {
const orderDetailsRepository = dbConnection.getRepository(OrderDetails);

Expand Down Expand Up @@ -501,3 +500,7 @@ export const getBestSellingProducts = async (req: Request, res: Response) => {

res.json(result);
};




2 changes: 1 addition & 1 deletion src/database/models/orderEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Order {
@Column({ nullable: true })
deliveryInfo: string;

@Column({ nullable: true })
@Column({ nullable: false })
country: string;

@Column({ nullable: true })
Expand Down
19 changes: 17 additions & 2 deletions src/docs/categoryDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,12 @@
* description: Internal Server Error
*/


/**
* @swagger
* /api/v1/category/get_metrics:
* get:
* summary: Get category metrics
* tags: [User]
* tags: [Category]
* security:
* - bearerAuth: []
* responses:
Expand All @@ -224,3 +223,19 @@
* description: Internal Server Error
*/

/**
* @swagger
* /api/v1/category/getSalesByCountry:
* get:
* summary: Get sales by country metrics
* tags: [Category]
* security:
* - bearerAuth: []
* responses:
* '200':
* description: Successful
* '401':
* description: Unauthorized
* '500':
* description: Internal Server Error
*/
11 changes: 8 additions & 3 deletions src/routes/categoryRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import {
getAllCategories,
getCategory,
getCategoryMetrics,
getSalesByCountry,
updateCategory,
} from '../controller/categoryController';
import { IsLoggedIn } from '../middlewares/isLoggedIn';
import { checkRole } from '../middlewares/authorize';

const categoryRouter = Router();

categoryRouter.route('/get_metrics').get(IsLoggedIn, checkRole(['Admin']), getCategoryMetrics)
categoryRouter
.route('/get_metrics')
.get(IsLoggedIn, checkRole(['Admin']), getCategoryMetrics);
categoryRouter
.route('/getSalesByCountry')
.get(IsLoggedIn, checkRole(['Admin']), getSalesByCountry);

categoryRouter
.route('/')
Expand All @@ -24,5 +30,4 @@ categoryRouter
.put(IsLoggedIn, updateCategory)
.delete(IsLoggedIn, deleteCategory);


export default categoryRouter;
export default categoryRouter;

0 comments on commit a619c5c

Please sign in to comment.