diff --git a/src/Database/Schemas/Invoices.ts b/src/Database/Schemas/Invoices.ts index 24f52bd..791798e 100644 --- a/src/Database/Schemas/Invoices.ts +++ b/src/Database/Schemas/Invoices.ts @@ -1,7 +1,6 @@ import mongoose, { Document, model, Schema } from "mongoose" import increment from "mongoose-auto-increment"; import { MongoDB_URI } from "../../Config"; -import mainEvent from "../../Events/Main"; import { IInvoice } from "../../Interfaces/Invoice"; const InvoiceSchame = new Schema @@ -81,24 +80,6 @@ InvoiceSchame.plugin(increment.plugin, { incrementBy: 1 }); -// Emit event when a new invoice is created -InvoiceSchame.post("save", function(invoice: IInvoice) -{ - mainEvent.emit("invoice_created", invoice); -}); - -// Emit event when an invoice is updated -InvoiceSchame.post("update", function(invoice: IInvoice) -{ - mainEvent.emit("invoice_updated", invoice); -}); - -// Emit event when an invoice is deleted -InvoiceSchame.post("remove", function(invoice: IInvoice) -{ - mainEvent.emit("invoice_deleted", invoice); -}); - const InvoiceModel = model("invoices", InvoiceSchame); export default InvoiceModel; \ No newline at end of file diff --git a/src/Database/Schemas/Orders.ts b/src/Database/Schemas/Orders.ts index 88a2c40..7625fb6 100644 --- a/src/Database/Schemas/Orders.ts +++ b/src/Database/Schemas/Orders.ts @@ -1,7 +1,6 @@ import mongoose, { Document, model, Schema } from "mongoose" import increment from "mongoose-auto-increment"; import { MongoDB_URI } from "../../Config"; -import mainEvent from "../../Events/Main"; import { IOrder } from "../../Interfaces/Orders"; const OrderSchame = new Schema @@ -75,24 +74,6 @@ OrderSchame.plugin(increment.plugin, { incrementBy: 1 }); -// Emit event when a new order is created -OrderSchame.post("save", function(order: IOrder) -{ - mainEvent.emit("order_created", order); -}); - -// Emit event when an order is updated -OrderSchame.post("update", function(order: IOrder) -{ - mainEvent.emit("order_updated", order); -}); - -// Emit event when an order is deleted -OrderSchame.post("remove", function(order: IOrder) -{ - mainEvent.emit("order_deleted", order); -}); - const OrderModel = model("orders", OrderSchame); export default OrderModel; \ No newline at end of file diff --git a/src/Interfaces/Events/MainOnEvents.ts b/src/Interfaces/Events/MainOnEvents.ts index 3fd72cf..95ee2c9 100644 --- a/src/Interfaces/Events/MainOnEvents.ts +++ b/src/Interfaces/Events/MainOnEvents.ts @@ -1,5 +1,11 @@ +import { ICategory } from "../Categories"; +import { IConfigurableOptions } from "../ConfigurableOptions"; +import { ICustomer } from "../Customer"; +import { IImage } from "../Images"; import { IInvoice } from "../Invoice"; import { IOrder } from "../Orders"; +import { IProduct } from "../Products"; +import { ITransactions } from "../Transactions"; export interface MainOnEvents { @@ -8,7 +14,32 @@ export interface MainOnEvents invoice_updated: IInvoice; invoice_paid: IInvoice; invoice_notified: IInvoice; + order_created: IOrder; order_updated: IOrder; order_deleted: IOrder; + + categories_created: ICategory; + categories_updated: ICategory; + categories_deleted: ICategory; + + product_created: IProduct; + product_updated: IProduct; + product_deleted: IProduct; + + customer_created: ICustomer; + customer_updated: ICustomer; + customer_deleted: ICustomer; + + images_created: IImage; + images_updated: IImage; + images_deleted: IImage; + + transaction_created: ITransactions; + transaction_updated: ITransactions; + transaction_deleted: ITransactions; + + configurable_options_created: IConfigurableOptions; + configurable_options_updated: IConfigurableOptions; + configurable_options_deleted: IConfigurableOptions; } \ No newline at end of file diff --git a/src/Lib/Orders/newInvoice.ts b/src/Lib/Orders/newInvoice.ts index 594ea6b..cafab07 100644 --- a/src/Lib/Orders/newInvoice.ts +++ b/src/Lib/Orders/newInvoice.ts @@ -8,6 +8,7 @@ import getCategoryByProduct from "../Products/getCategoryByProduct"; import { IProduct } from "../../Interfaces/Products"; import ConfigurableOptionsModel from "../../Database/Schemas/ConfigurableOptions"; import { IConfigurableOptions } from "../../Interfaces/ConfigurableOptions"; +import mainEvent from "../../Events/Main"; // Create a method that checks if the order next recycle is within 14 days @@ -94,6 +95,8 @@ export async function createInvoiceFromOrder(order: IOrder) notified: false, })).save(); + mainEvent.emit("invoice_created", newInvoice); + return newInvoice; } diff --git a/src/Models/BaseModelAPI.ts b/src/Models/BaseModelAPI.ts index 1ce621d..9992668 100644 --- a/src/Models/BaseModelAPI.ts +++ b/src/Models/BaseModelAPI.ts @@ -59,10 +59,10 @@ export default class BaseModelAPI public findAndPatch( uid: IModel["uid"], data: IModel - ): Promise + ): Promise { if(!uid || uid === "undefined") - return Promise.resolve([]); + return Promise.reject("No uid provided"); return this.iModel.findOneAndUpdate({ $or: [ { id: uid }, { uid: uid } diff --git a/src/Routes/v2/Categories/Categories.controller.ts b/src/Routes/v2/Categories/Categories.controller.ts index 85116c3..cc8fff1 100644 --- a/src/Routes/v2/Categories/Categories.controller.ts +++ b/src/Routes/v2/Categories/Categories.controller.ts @@ -1,6 +1,7 @@ import { Request, Response } from "express"; import CategoryModel from "../../../Database/Schemas/Category"; import ProductModel from "../../../Database/Schemas/Products"; +import mainEvent from "../../../Events/Main"; import { ICategory } from "../../../Interfaces/Categories"; import { idCategory } from "../../../Lib/Generator"; import { APISuccess } from "../../../Lib/Response"; @@ -12,6 +13,9 @@ function insert(req: Request, res: Response) { API.create(req.body) .then((result) => { + + mainEvent.emit("categories_created", result); + APISuccess({ uid: result.uid })(res); @@ -48,6 +52,8 @@ function list(req: Request, res: Response) function patch(req: Request, res: Response) { API.findAndPatch((req.params.uid as ICategory["uid"]), req.body).then((result) => { + // @ts-ignore + mainEvent.emit("categories_updated", result); APISuccess(result)(res); }); } @@ -55,7 +61,9 @@ function patch(req: Request, res: Response) function removeById(req: Request, res: Response) { API.removeByUid(req.params.userId as ICategory["uid"]) - .then((result)=>{ + .then((result)=> { + // @ts-ignore + mainEvent.emit("categories_deleted", result); APISuccess({}, 204)(res) }); }; diff --git a/src/Routes/v2/ConfigurableOptions/ConfigurableOptions.controller.ts b/src/Routes/v2/ConfigurableOptions/ConfigurableOptions.controller.ts index 9fc4089..7b211a0 100644 --- a/src/Routes/v2/ConfigurableOptions/ConfigurableOptions.controller.ts +++ b/src/Routes/v2/ConfigurableOptions/ConfigurableOptions.controller.ts @@ -1,5 +1,6 @@ import { Request, Response } from "express"; import ConfigurableOptionsModel from "../../../Database/Schemas/ConfigurableOptions"; +import mainEvent from "../../../Events/Main"; import { IConfigurableOptions } from "../../../Interfaces/ConfigurableOptions"; import { idConfigurableOptions } from "../../../Lib/Generator"; import { APISuccess } from "../../../Lib/Response"; @@ -11,6 +12,9 @@ function insert(req: Request, res: Response) { API.create(req.body) .then((result) => { + + mainEvent.emit("configurable_options_created", result); + APISuccess({ uid: result.uid })(res); @@ -47,6 +51,8 @@ function list(req: Request, res: Response) function patch(req: Request, res: Response) { API.findAndPatch((req.params.uid as IConfigurableOptions["uid"]), req.body).then((result) => { + // @ts-ignore + mainEvent.emit("configurable_options_updated", result); APISuccess(result)(res); }); } @@ -55,6 +61,8 @@ function removeById(req: Request, res: Response) { API.removeByUid(req.params.uid as IConfigurableOptions["uid"]) .then((result)=>{ + // @ts-ignore + mainEvent.emit("configurable_options_deleted", result); APISuccess(result, 204)(res) }); }; diff --git a/src/Routes/v2/Customers/Customers.controller.ts b/src/Routes/v2/Customers/Customers.controller.ts index ab7221f..dcfc46c 100644 --- a/src/Routes/v2/Customers/Customers.controller.ts +++ b/src/Routes/v2/Customers/Customers.controller.ts @@ -10,6 +10,7 @@ import { SendEmail } from "../../../Email/Send"; import { Company_Name } from "../../../Config"; import Footer from "../../../Email/Templates/General/Footer"; import getFullName from "../../../Lib/Customers/getFullName"; +import mainEvent from "../../../Events/Main"; const API = new BaseModelAPI(idCustomer, CustomerModel); @@ -32,6 +33,8 @@ function insert(req: Request, res: Response) API.create(req.body) .then((result) => { + + mainEvent.emit("customer_created", result); // Send email to customer SendEmail(result.personal.email, `Welcome to ${Company_Name}`, { @@ -95,6 +98,8 @@ async function patch(req: Request, res: Response) } } API.findAndPatch((req.params.uid as ICustomer["uid"]), req.body).then((result) => { + // @ts-ignore + mainEvent.emit("customer_updated", result); APISuccess(result)(res); }); } @@ -103,6 +108,8 @@ function removeById(req: Request, res: Response) { API.removeByUid(req.params.uid as ICustomer["uid"]) .then((result)=>{ + // @ts-ignore + mainEvent.emit("customer_deleted", result); APISuccess(result, 204)(res) }); }; diff --git a/src/Routes/v2/Invoices/Invoices.controller.ts b/src/Routes/v2/Invoices/Invoices.controller.ts index 0c318eb..8116d95 100644 --- a/src/Routes/v2/Invoices/Invoices.controller.ts +++ b/src/Routes/v2/Invoices/Invoices.controller.ts @@ -1,8 +1,9 @@ import { Request, Response } from "express"; import InvoiceModel from "../../../Database/Schemas/Invoices"; +import mainEvent from "../../../Events/Main"; import { IInvoice } from "../../../Interfaces/Invoice"; import { idInvoice } from "../../../Lib/Generator"; -import { APISuccess } from "../../../Lib/Response"; +import { APIError, APISuccess } from "../../../Lib/Response"; import BaseModelAPI from "../../../Models/BaseModelAPI"; const API = new BaseModelAPI(idInvoice, InvoiceModel); @@ -11,6 +12,9 @@ function insert(req: Request, res: Response) { API.create(req.body) .then((result) => { + + mainEvent.emit("invoice_created", result); + APISuccess({ uid: result.uid })(res); @@ -46,8 +50,15 @@ function list(req: Request, res: Response) function patch(req: Request, res: Response) { + const paid = req.body.paid ?? false; API.findAndPatch((req.params.uid as IInvoice["uid"]), req.body).then((result) => { + if(paid !== result.paid && result.paid) + mainEvent.emit("invoice_paid", result); + // @ts-ignore + mainEvent.emit("invoice_updated", result); APISuccess(result)(res); + }).catch((err) => { + APIError(err)(res); }); } @@ -55,6 +66,8 @@ function removeById(req: Request, res: Response) { API.removeByUid(req.params.uid as IInvoice["uid"]) .then((result)=>{ + // @ts-ignore + mainEvent.emit("invoice_deleted", result); APISuccess(result, 204)(res) }); }; diff --git a/src/Routes/v2/Orders/Orders.config.ts b/src/Routes/v2/Orders/Orders.config.ts index 319c15d..5bac6ff 100644 --- a/src/Routes/v2/Orders/Orders.config.ts +++ b/src/Routes/v2/Orders/Orders.config.ts @@ -21,6 +21,7 @@ import NewOrderCreated from "../../../Email/Templates/Orders/NewOrderCreated"; import { IConfigurableOptions } from "../../../Interfaces/ConfigurableOptions"; import { CreatePaymentIntent } from "../../../Payments/Stripe"; import { createSwishQRCode } from "../../../Payments/Swish"; +import mainEvent from "../../../Events/Main"; async function createOrder(customer: ICustomer, products: Array<{ product_id: IProduct["id"], @@ -55,6 +56,8 @@ async function createOrder(customer: ICustomer, products: Array<{ uid: idOrder(), }).save()); + mainEvent.emit("order_created", order); + SendEmail(customer.personal.email, `New order from ${Company_Name !== "" ? Company_Name : "CPG"} #${order.id}`, { isHTML: true, body: NewOrderCreated(order, customer), diff --git a/src/Routes/v2/Orders/Orders.controller.ts b/src/Routes/v2/Orders/Orders.controller.ts index 81c20aa..5275890 100644 --- a/src/Routes/v2/Orders/Orders.controller.ts +++ b/src/Routes/v2/Orders/Orders.controller.ts @@ -11,6 +11,7 @@ import { SendEmail } from "../../../Email/Send"; import CustomerModel from "../../../Database/Schemas/Customers/Customer"; import NewOrderCreated from "../../../Email/Templates/Orders/NewOrderCreated"; import { Company_Name } from "../../../Config"; +import mainEvent from "../../../Events/Main"; const API = new BaseModelAPI(idOrder, OrderModel); @@ -36,6 +37,8 @@ async function insert(req: Request, res: Response) API.create(req.body) .then(async (result) => { + mainEvent.emit("order_created", result); + const customer = await CustomerModel.findOne({ id: result.customer_uid }); if(customer) @@ -80,6 +83,8 @@ function list(req: Request, res: Response) function patch(req: Request, res: Response) { API.findAndPatch((req.params.uid as IOrder["uid"]), req.body).then((result) => { + // @ts-ignore + mainEvent.emit("order_updated", result); APISuccess(result)(res); }); } @@ -88,6 +93,8 @@ function removeById(req: Request, res: Response) { API.removeByUid(req.params.uid as IOrder["uid"]) .then((result)=>{ + // @ts-ignore + mainEvent.emit("order_deleted", result); APISuccess(result, 204)(res) }); }; diff --git a/src/Routes/v2/Products/Products.controller.ts b/src/Routes/v2/Products/Products.controller.ts index 45fbee7..f218d62 100644 --- a/src/Routes/v2/Products/Products.controller.ts +++ b/src/Routes/v2/Products/Products.controller.ts @@ -1,5 +1,6 @@ import { Request, Response } from "express"; import ProductModel from "../../../Database/Schemas/Products"; +import mainEvent from "../../../Events/Main"; import { IProduct } from "../../../Interfaces/Products"; import { idProduct } from "../../../Lib/Generator"; import { APISuccess } from "../../../Lib/Response"; @@ -11,6 +12,9 @@ function insert(req: Request, res: Response) { API.create(req.body) .then((result) => { + + mainEvent.emit("product_created", result); + APISuccess({ uid: result.uid })(res); @@ -47,6 +51,8 @@ function list(req: Request, res: Response) function patch(req: Request, res: Response) { API.findAndPatch((req.params.uid as IProduct["uid"]), req.body).then((result) => { + // @ts-ignore + mainEvent.emit("product_updated", result); APISuccess(result)(res); }); } @@ -55,6 +61,8 @@ function removeById(req: Request, res: Response) { API.removeByUid(req.params.uid as IProduct["uid"]) .then((result)=>{ + // @ts-ignore + mainEvent.emit("product_deleted", result); APISuccess(result, 204)(res) }); }; diff --git a/src/Routes/v2/Stripe/Stripe.config.ts b/src/Routes/v2/Stripe/Stripe.config.ts index 3bd8beb..51976bd 100644 --- a/src/Routes/v2/Stripe/Stripe.config.ts +++ b/src/Routes/v2/Stripe/Stripe.config.ts @@ -207,7 +207,6 @@ export default class StripeRouter switch (intent.status) { case 'succeeded': message = 'Success! Payment received.'; - invoice.paid = true; href = Company_Website; status = intent.status; break; diff --git a/src/Routes/v2/Transactions/Transactions.controller.ts b/src/Routes/v2/Transactions/Transactions.controller.ts index 82a5576..17ddb07 100644 --- a/src/Routes/v2/Transactions/Transactions.controller.ts +++ b/src/Routes/v2/Transactions/Transactions.controller.ts @@ -1,5 +1,6 @@ import { Request, Response } from "express"; import TransactionsModel from "../../../Database/Schemas/Transactions"; +import mainEvent from "../../../Events/Main"; import { ITransactions } from "../../../Interfaces/Transactions"; import { idTransicitons } from "../../../Lib/Generator"; import { APISuccess } from "../../../Lib/Response"; @@ -11,6 +12,9 @@ function insert(req: Request, res: Response) { API.create(req.body) .then((result) => { + + mainEvent.emit("transaction_created", result); + APISuccess({ uid: result.uid })(res); @@ -47,6 +51,8 @@ function list(req: Request, res: Response) function patch(req: Request, res: Response) { API.findAndPatch((req.params.uid as ITransactions["uid"]), req.body).then((result) => { + // @ts-ignore + mainEvent.emit("transaction_updated", result); APISuccess(result)(res); }); } @@ -55,6 +61,8 @@ function removeById(req: Request, res: Response) { API.removeByUid(req.params.uid as ITransactions["uid"]) .then((result)=>{ + // @ts-ignore + mainEvent.emit("transaction_deleted", result); APISuccess(result, 204)(res) }); };