Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

v0.9 #50

Merged
merged 3 commits into from
Dec 19, 2021
Merged

v0.9 #50

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: 0 additions & 1 deletion src/Lib/Orders/newInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export async function getNewPriceOfPromotionCode(code: IPromotionsCodes & Docume
// Convert string to date
if(new Date(code.valid_to) < new Date())
{
Logger.debug(code.valid_to, new Date());
Logger.debug(`Promotion code ${code.name} got invalid valid date`);
return product;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Lib/Response.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Response } from "express";
import Logger from "./Logger";

export function APISuccess(msg: any, status: number = 200)
{
return (res: Response) => {
let ip = res.req.headers['x-forwarded-for'] || res.req.socket.remoteAddress;
let url = res.req.originalUrl;
let method = res.req.method;
Logger.api(`${ip} ${method} ${url} ${status}`);
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
res.setHeader("X-Total-Count", msg?.length ?? 0)
res.status(status).json(msg);
Expand All @@ -12,6 +17,10 @@ export function APISuccess(msg: any, status: number = 200)
export function APIError(msg: any, status: number = 400)
{
return (res: Response) => {
let ip = res.req.headers['x-forwarded-for'] || res.req.socket.remoteAddress;
let url = res.req.url;
let method = res.req.method;
Logger.api(`${ip} ${method} ${url} ${status}`);
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
res.setHeader("X-Total-Count", msg?.length ?? 0)
res.status(status).json(msg);
Expand Down
93 changes: 93 additions & 0 deletions src/Routes/v2/PromotionsCodes/PromotionCode.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Application, Router } from "express";
import ProductModel from "../../../Database/Schemas/Products";
import PromotionCodeModel from "../../../Database/Schemas/PromotionsCode";
import Logger from "../../../Lib/Logger";
import { APIError, APISuccess } from "../../../Lib/Response";
import EnsureAdmin from "../../../Middlewares/EnsureAdmin";
import PromotionCodeController from "./PromotionCode.controller";

Expand All @@ -22,6 +26,95 @@ export default class PromotionCodeRoute
PromotionCodeController.getByUid
]);

this.router.get("/:name/apply", async (req, res) => {
const name = req.params.name;
const _products = req.body.products as number[];

if(!_products)
return APIError("Missing products", 400)(res);

const products = await ProductModel.find({
id: {
$in: _products
}
});

const code = await PromotionCodeModel.findOne({ name: name });
if(!code)
return APIError(`Promotion code ${name} not found`, 404)(res);

// Check if code is valid
if(code.valid_to !== "permament")
// Convert string to date
if(new Date(code.valid_to) < new Date())
{
Logger.debug(`Promotion code ${code.name} got invalid valid date`);
return APIError(`Promotion code ${name} got invalid valid date`, 400)(res);
}

if(typeof code.valid_to === "string")
if(code.uses <= 0)
{
Logger.warning(`Promotion code ${code.name} has no uses left`);
return APIError(`Promotion code ${name} has no uses left`, 400)(res);
}

Logger.info(`Promotion code ${code.name} (${code.id}) is valid`);

// Filter products which is not included in the promotion code
const filtered_products = products.filter(product => {
if(code.products_ids.includes(product.id))
{
Logger.info(`Promotion code ${code.name} (${code.id}) is valid for product ${product.id}`);
return true;
}
else
{
Logger.info(`Promotion code ${code.name} (${code.id}) is not valid for product ${product.id}`);
return false;
}
});

let new_changed_products = [];

// Loop through each product
for(const product of filtered_products)
{
if(code.products_ids.includes(product.id))
{
Logger.info(`Promotion code ${code.name} (${code.id}) is valid for product ${product.id}`);
let o_price = product.price;
if(code.procentage)
product.price = product.price+(product.price*code.discount);
else
product.price = product.price-code.discount;

Logger.info(`New price of product ${product.id} is ${product.price}, old price was ${o_price}`);
// Check if we are - on price
if(product.price < 0)
{
Logger.error(`Product ${product.id} price is less than 0, making it "free" by setting it to 0`);
product.price = 0;
}
new_changed_products.push({
...product,
o_price
});
}
}

if(new_changed_products.length === 0)
return APIError(`Promotion code ${name} is not valid for any of the products`, 400)(res);

APISuccess(new_changed_products.map(p => {
return {
id: p.id,
new_price: p.price,
old_price: p.o_price
}
}))(res);
});

this.router.post("/", [
EnsureAdmin,
PromotionCodeController.insert
Expand Down