From 31e7653081d5ee6b6b5ed96d2df4c5c0daa6ebad Mon Sep 17 00:00:00 2001 From: Icyeza Date: Wed, 22 May 2024 17:10:37 +0200 Subject: [PATCH 1/4] implementing order tracking and order managment issue implementing order tracking and order managment issue implementing order tracking and order managment issue implementing order tracking and order managment issue adding documentation and tests writing tests --- .github/workflows/ci.yml | 1 - package.json | 2 + src/__test__/cart.test.ts | 17 +- src/__test__/coupon.test.ts | 80 ++-- src/__test__/errorHandler.test.ts | 78 ++-- src/__test__/getProduct.test.ts | 2 +- src/__test__/isAllowed.test.ts | 29 +- src/__test__/logout.test.ts | 3 +- src/__test__/oauth.test.ts | 24 +- src/__test__/orderManagement.test.ts | 390 ++++++++++++++++++ src/__test__/productStatus.test.ts | 14 +- src/__test__/roleCheck.test.ts | 2 +- src/__test__/route.test.ts | 3 +- src/__test__/test-assets/DatabaseCleanup.ts | 28 +- src/__test__/userServices.test.ts | 6 +- src/__test__/userStatus.test.ts | 2 +- src/__test__/vendorProduct.test.ts | 2 +- src/__test__/wishList.test.ts | 2 +- src/controllers/adminOrdercontroller.ts | 18 + src/controllers/couponController.ts | 4 +- src/controllers/index.ts | 4 +- src/controllers/orderController.ts | 2 +- src/controllers/productController.ts | 23 +- src/controllers/vendorOrderController.ts | 14 + src/controllers/wishListController.ts | 29 +- src/docs/adminOrderManagement.yml | 80 ++++ src/docs/couponDocs.yml | 2 +- src/docs/vendorOrderManagement.yml | 93 +++++ src/docs/vendorProduct.yml | 14 +- src/docs/wishListDocs.yml | 2 +- src/entities/Cart.ts | 2 +- src/entities/CartItem.ts | 2 +- src/entities/Order.ts | 24 +- src/entities/Product.ts | 6 +- src/entities/User.ts | 4 +- src/entities/VendorOrderItem.ts | 30 ++ src/entities/transaction.ts | 2 +- src/entities/vendorOrders.ts | 49 +++ src/entities/wishList.ts | 17 +- src/index.ts | 29 +- src/routes/ProductRoutes.ts | 20 +- src/routes/UserRoutes.ts | 39 +- src/routes/couponRoutes.ts | 13 +- src/routes/index.ts | 4 +- src/routes/wishListRoute.ts | 27 +- src/services/adminOrderServices/readOrder.ts | 158 +++++++ .../adminOrderServices/updateOrder.ts | 107 +++++ .../couponServices/buyerApplyCoupon.ts | 134 +++--- src/services/index.ts | 8 + src/services/orderServices/createOrder.ts | 63 ++- src/services/orderServices/getOrderService.ts | 104 +++-- .../orderServices/updateOrderService.ts | 231 ++++++----- src/services/productServices/deleteProduct.ts | 40 +- .../getRecommendedProductsService.ts | 105 ++--- .../productServices/listAllProductsService.ts | 64 +-- src/services/productServices/readProduct.ts | 116 +++--- .../productServices/removeProductImage.ts | 2 +- src/services/productServices/searchProduct.ts | 7 +- .../productServices/viewSingleProduct.ts | 45 +- .../vendorOrderServices/readVendorOrder.ts | 119 ++++++ .../vendorOrderServices/updateVendorOrder.ts | 87 ++++ src/services/wishListServices/addProduct.ts | 89 ++-- src/services/wishListServices/clearAll.ts | 27 +- src/services/wishListServices/getProducts.ts | 59 +-- .../wishListServices/removeProducts.ts | 30 +- src/startups/getSwaggerServer.ts | 2 +- src/utils/auth.ts | 104 +++-- src/utils/index.ts | 26 +- src/utils/sendOrderMail.ts | 15 +- src/utils/sendOrderMailUpdated.ts | 15 +- src/utils/socket.ts | 21 + tsconfig.json | 214 +++++----- 72 files changed, 2260 insertions(+), 970 deletions(-) create mode 100644 src/__test__/orderManagement.test.ts create mode 100644 src/controllers/adminOrdercontroller.ts create mode 100644 src/controllers/vendorOrderController.ts create mode 100644 src/docs/adminOrderManagement.yml create mode 100644 src/docs/vendorOrderManagement.yml create mode 100644 src/entities/VendorOrderItem.ts create mode 100644 src/entities/vendorOrders.ts create mode 100644 src/services/adminOrderServices/readOrder.ts create mode 100644 src/services/adminOrderServices/updateOrder.ts create mode 100644 src/services/vendorOrderServices/readVendorOrder.ts create mode 100644 src/services/vendorOrderServices/updateVendorOrder.ts create mode 100644 src/utils/socket.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e0c5f52..224592b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,6 @@ env: CLOUDINARY_API_SECRET: ${{secrets.CLOUDINARY_API_SECRET}} GOOGLE_CLIENT_ID: ${{secrets.GOOGLE_CLIENT_ID}} GOOGLE_CLIENT_SECRET: ${{secrets.GOOGLE_CLIENT_SECRET}} - jobs: build-lint-test-coverage: diff --git a/package.json b/package.json index 06e7e40..ef7acc5 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "passport-google-oauth20": "^2.0.0", "pg": "^8.11.5", "reflect-metadata": "^0.2.2", + "socket.io": "^4.7.5", "source-map-support": "^0.5.21", "superagent": "^9.0.1", "swagger-jsdoc": "^6.2.8", @@ -78,6 +79,7 @@ "@types/nodemailer": "^6.4.15", "@types/passport-google-oauth20": "^2.0.16", "@types/reflect-metadata": "^0.1.0", + "@types/socket.io": "^3.0.2", "@types/supertest": "^6.0.2", "@types/uuid": "^9.0.8", "@types/winston": "^2.4.4", diff --git a/src/__test__/cart.test.ts b/src/__test__/cart.test.ts index 9f86f73..4d6d1f0 100644 --- a/src/__test__/cart.test.ts +++ b/src/__test__/cart.test.ts @@ -1,4 +1,3 @@ - import request from 'supertest'; import jwt from 'jsonwebtoken'; import { app, server } from '../index'; @@ -171,8 +170,9 @@ beforeAll(async () => { }); afterAll(async () => { - await cleanDatabase() + await cleanDatabase(); + server.close(); }); describe('Cart management for guest/buyer', () => { @@ -524,12 +524,12 @@ describe('Order management tests', () => { city: 'Test City', street: 'Test Street', }, - }).set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + }) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); expect(response.status).toBe(400); }); it('should create a new order', async () => { - const response = await request(app) .post('/product/orders') .send({ @@ -547,7 +547,6 @@ describe('Order management tests', () => { }); it('should insert a new order', async () => { - const response = await request(app) .post('/product/orders') .send({ @@ -570,9 +569,8 @@ describe('Order management tests', () => { const response = await request(app) .get('/product/client/orders') .set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`); - expect(response.status).toBe(404); + expect(response.status).toBe(404); expect(response.body.message).toBeUndefined; - }); it('should return 404 if the buyer has no orders', async () => { @@ -586,13 +584,11 @@ describe('Order management tests', () => { describe('Get transaction history', () => { it('should return transaction history for the buyer', async () => { - const response = await request(app) .get('/product/orders/history') .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); expect(response.status).toBe(404); expect(response.body.message).toBe('No transaction history found'); - }); it('should return 400 when user ID is not provided', async () => { @@ -605,12 +601,11 @@ describe('Order management tests', () => { describe('Update order', () => { it('should update order status successfully', async () => { - const response = await request(app) .put(`/product/client/orders/${orderId}`) .send({ orderStatus: 'delivered' }) .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); - expect(response.status).toBe(500); + expect(response.status).toBe(500); }); }); }); diff --git a/src/__test__/coupon.test.ts b/src/__test__/coupon.test.ts index b3f68b4..269e95e 100644 --- a/src/__test__/coupon.test.ts +++ b/src/__test__/coupon.test.ts @@ -21,7 +21,7 @@ const product2Id = uuid(); const couponCode = 'DISCOUNT20'; const couponCode1 = 'DISCOUNT10'; const couponCode2 = 'DISCOUNT99'; -const couponCode3 = 'DISCOUNT22' +const couponCode3 = 'DISCOUNT22'; const expiredCouponCode = 'EXPIRED'; const finishedCouponCode = 'FINISHED'; const moneyCouponCode = 'MONEY'; @@ -199,11 +199,10 @@ beforeAll(async () => { const cartItemRepository = connection?.getRepository(CartItem); await cartItemRepository?.save({ ...sampleCartItem1 }); - }); afterAll(async () => { - await cleanDatabase() + await cleanDatabase(); server.close(); }); @@ -340,84 +339,87 @@ describe('Coupon Management System', () => { }); describe('Buyer Coupon Application', () => { - describe('Checking Coupon Conditions', () =>{ + describe('Checking Coupon Conditions', () => { it('should return 400 when no coupon submitted', async () => { const response = await request(app) .post(`/coupons/apply`) .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); - expect(response.status).toBe(400); - expect(response.body.message).toBe('Coupon Code is required'); - }) + expect(response.status).toBe(400); + expect(response.body.message).toBe('Coupon Code is required'); + }); it('should return 404 if coupon code is not found in the database', async () => { const response = await request(app) .post(`/coupons/apply`) .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) .send({ - couponCode: "InvalidCode", + couponCode: 'InvalidCode', }); - expect(response.status).toBe(404); - expect(response.body.message).toBe('Invalid Coupon Code'); - }) + expect(response.status).toBe(404); + expect(response.body.message).toBe('Invalid Coupon Code'); + }); it('should not allow use of expired tokens', async () => { const response = await request(app) - .post(`/coupons/apply`) + .post(`/coupons/apply`) .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) .send({ couponCode: expiredCoupon.code, }); - expect(response.status).toBe(400); - expect(response.body.message).toBe('Coupon is expired'); - }) + expect(response.status).toBe(400); + expect(response.body.message).toBe('Coupon is expired'); + }); it('should not allow use of coupon that reach maximum users', async () => { const response = await request(app) - .post(`/coupons/apply`) + .post(`/coupons/apply`) .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) .send({ couponCode: finishedCoupon.code, }); - expect(response.status).toBe(400); - expect(response.body.message).toBe('Coupon Discount Ended'); - }) + expect(response.status).toBe(400); + expect(response.body.message).toBe('Coupon Discount Ended'); + }); it('Should not work when the product is not in cart', async () => { const response = await request(app) - .post(`/coupons/apply`) - .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) - .send({ + .post(`/coupons/apply`) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) + .send({ couponCode: sampleCoupon3.code, }); expect(response.status).toBe(404); - expect(response.body.message).toBe("No product in Cart with that coupon code"); - }) - }) + expect(response.body.message).toBe('No product in Cart with that coupon code'); + }); + }); - describe("Giving discount according the the product coupon", () => { + describe('Giving discount according the the product coupon', () => { it('Should give discont when discount-type is percentage', async () => { const response = await request(app) - .post(`/coupons/apply`) - .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) - .send({ + .post(`/coupons/apply`) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) + .send({ couponCode: sampleCoupon2.code, }); expect(response.status).toBe(200); - expect(response.body.message).toBe(`Coupon Code successfully activated discount on product: ${sampleProduct1.name}`); - }) + expect(response.body.message).toBe( + `Coupon Code successfully activated discount on product: ${sampleProduct1.name}` + ); + }); it('Should give discont when discount-type is money', async () => { const response = await request(app) - .post(`/coupons/apply`) - .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) - .send({ + .post(`/coupons/apply`) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) + .send({ couponCode: moneyCoupon.code, }); expect(response.status).toBe(200); - expect(response.body.message).toBe(`Coupon Code successfully activated discount on product: ${sampleProduct1.name}`); - }) - }) - -}) + expect(response.body.message).toBe( + `Coupon Code successfully activated discount on product: ${sampleProduct1.name}` + ); + }); + }); +}); diff --git a/src/__test__/errorHandler.test.ts b/src/__test__/errorHandler.test.ts index fb1437c..cf079f0 100644 --- a/src/__test__/errorHandler.test.ts +++ b/src/__test__/errorHandler.test.ts @@ -1,47 +1,47 @@ import { Request, Response } from 'express'; -import { CustomError, errorHandler } from '../middlewares/errorHandler' +import { CustomError, errorHandler } from '../middlewares/errorHandler'; describe('CustomError', () => { - it('should create a CustomError object with statusCode and status properties', () => { - const message = 'Test error message'; - const statusCode = 404; - const customError = new CustomError(message, statusCode); - expect(customError.message).toBe(message); - expect(customError.statusCode).toBe(statusCode); - expect(customError.status).toBe('fail'); - }); + it('should create a CustomError object with statusCode and status properties', () => { + const message = 'Test error message'; + const statusCode = 404; + const customError = new CustomError(message, statusCode); + expect(customError.message).toBe(message); + expect(customError.statusCode).toBe(statusCode); + expect(customError.status).toBe('fail'); }); +}); - describe('errorHandler', () => { - it('should send correct response with status code and message', () => { - const err = new CustomError('Test error message', 404); - const req = {} as Request; - const res = { - status: jest.fn().mockReturnThis(), - json: jest.fn(), - } as unknown as Response; - const next = jest.fn(); - errorHandler(err, req, res, next); - expect(res.status).toHaveBeenCalledWith(404); - expect(res.json).toHaveBeenCalledWith({ - status: 404, - message: 'Test error message', - }); +describe('errorHandler', () => { + it('should send correct response with status code and message', () => { + const err = new CustomError('Test error message', 404); + const req = {} as Request; + const res = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + } as unknown as Response; + const next = jest.fn(); + errorHandler(err, req, res, next); + expect(res.status).toHaveBeenCalledWith(404); + expect(res.json).toHaveBeenCalledWith({ + status: 404, + message: 'Test error message', }); - it('should handle errors with status code 500', () => { - const err = new CustomError('something went wrong', 500); - const req = {} as Request; - const res = { - status: jest.fn().mockReturnThis(), - json: jest.fn(), - } as unknown as Response; - const next = jest.fn(); - errorHandler(err, req, res, next); + }); + it('should handle errors with status code 500', () => { + const err = new CustomError('something went wrong', 500); + const req = {} as Request; + const res = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + } as unknown as Response; + const next = jest.fn(); + errorHandler(err, req, res, next); - expect(res.status).toHaveBeenCalledWith(500); - expect(res.json).toHaveBeenCalledWith({ - status: 500, - message: 'something went wrong', - }); + expect(res.status).toHaveBeenCalledWith(500); + expect(res.json).toHaveBeenCalledWith({ + status: 500, + message: 'something went wrong', }); - }); \ No newline at end of file + }); +}); diff --git a/src/__test__/getProduct.test.ts b/src/__test__/getProduct.test.ts index 88dd415..ecd2281 100644 --- a/src/__test__/getProduct.test.ts +++ b/src/__test__/getProduct.test.ts @@ -68,7 +68,7 @@ beforeAll(async () => { }); afterAll(async () => { - await cleanDatabase() + await cleanDatabase(); server.close(); }); diff --git a/src/__test__/isAllowed.test.ts b/src/__test__/isAllowed.test.ts index b17b657..471a950 100644 --- a/src/__test__/isAllowed.test.ts +++ b/src/__test__/isAllowed.test.ts @@ -48,24 +48,23 @@ beforeAll(async () => { }); afterAll(async () => { - await cleanDatabase() - + await cleanDatabase(); }); describe('Middleware - checkUserStatus', () => { - beforeEach(() => { - reqMock = {}; - resMock = { - status: jest.fn().mockReturnThis(), - json: jest.fn() - }; - nextMock = jest.fn(); - }); - - it('should return 401 if user is not authenticated', async () => { - await checkUserStatus(reqMock as Request, resMock as Response, nextMock); - expect(responseError).toHaveBeenCalledWith(resMock, 401, 'Authentication required'); - }); + beforeEach(() => { + reqMock = {}; + resMock = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + }; + nextMock = jest.fn(); + }); + + it('should return 401 if user is not authenticated', async () => { + await checkUserStatus(reqMock as Request, resMock as Response, nextMock); + expect(responseError).toHaveBeenCalledWith(resMock, 401, 'Authentication required'); + }); it('should return 401 if user is not found', async () => { reqMock = { user: { id: uuid() } }; diff --git a/src/__test__/logout.test.ts b/src/__test__/logout.test.ts index cd950fd..ac9eefa 100644 --- a/src/__test__/logout.test.ts +++ b/src/__test__/logout.test.ts @@ -10,8 +10,7 @@ beforeAll(async () => { }); afterAll(async () => { - await cleanDatabase() - + await cleanDatabase(); server.close(); }); diff --git a/src/__test__/oauth.test.ts b/src/__test__/oauth.test.ts index 2493059..877d63b 100644 --- a/src/__test__/oauth.test.ts +++ b/src/__test__/oauth.test.ts @@ -5,24 +5,20 @@ import { User } from '../entities/User'; import { cleanDatabase } from './test-assets/DatabaseCleanup'; beforeAll(async () => { - await createConnection(); }); afterAll(async () => { - await cleanDatabase() + await cleanDatabase(); server.close(); }); -describe('authentication routes test',() => { - it('should redirect to the google authentication page',async() => { - const response = await request(app) - .get('/user/google-auth'); - expect(response.statusCode).toBe(302) - }) - it('should redirect after google authentication', async() => { - const response = await request(app) - .get('/user/auth/google/callback'); - expect(response.statusCode).toBe(302) - }) +describe('authentication routes test', () => { + it('should redirect to the google authentication page', async () => { + const response = await request(app).get('/user/google-auth'); + expect(response.statusCode).toBe(302); + }); + it('should redirect after google authentication', async () => { + const response = await request(app).get('/user/auth/google/callback'); + expect(response.statusCode).toBe(302); + }); }); - diff --git a/src/__test__/orderManagement.test.ts b/src/__test__/orderManagement.test.ts new file mode 100644 index 0000000..846b9d8 --- /dev/null +++ b/src/__test__/orderManagement.test.ts @@ -0,0 +1,390 @@ +import request from 'supertest'; +import jwt from 'jsonwebtoken'; +import { app, server } from '../index'; +import { getConnection } from 'typeorm'; +import { dbConnection } from '../startups/dbConnection'; +import { User, UserInterface } from '../entities/User'; +import { v4 as uuid } from 'uuid'; +import { Product } from '../entities/Product'; +import { Category } from '../entities/Category'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; +import { Order } from '../entities/Order'; +import { OrderItem } from '../entities/OrderItem'; +import { VendorOrders } from '../entities/vendorOrders'; +import { VendorOrderItem } from '../entities/VendorOrderItem'; + +const adminId = uuid(); +const vendorId = uuid(); +const vendor2Id = uuid(); +const buyerId = uuid(); + +const productId = uuid(); +const product2Id = uuid(); + +const orderId = uuid(); +const orderItemId = uuid(); +const order2Id = uuid(); +const order2ItemId = uuid(); + +const vendorOrderId = uuid(); +const vendorOrderItemId = uuid(); +const vendorOrder2Id = uuid(); +const vendorOrder2ItemId = uuid(); +const catId = uuid(); + +console.log(adminId, vendorId, buyerId); + +const jwtSecretKey = process.env.JWT_SECRET || ''; + +const getAccessToken = (id: string, email: string) => { + return jwt.sign( + { + id: id, + email: email, + }, + jwtSecretKey + ); +}; + +const sampleAdmin: UserInterface = { + id: adminId, + firstName: 'admin', + lastName: 'user', + email: 'admin@example.com', + password: 'password', + userType: 'Admin', + gender: 'Male', + phoneNumber: '126380997', + photoUrl: 'https://example.com/photo.jpg', + verified: true, + role: 'ADMIN', +}; +const sampleVendor: UserInterface = { + id: vendorId, + firstName: 'vendor', + lastName: 'user', + email: 'vendor@example.com', + password: 'password', + userType: 'Vendor', + gender: 'Male', + phoneNumber: '126380996347', + photoUrl: 'https://example.com/photo.jpg', + verified: true, + role: 'VENDOR', +}; +const sampleVendor2: UserInterface = { + id: vendor2Id, + firstName: 'vendor', + lastName: 'user', + email: 'vendor2@example.com', + password: 'password', + userType: 'Vendor', + gender: 'Male', + phoneNumber: '18090296347', + photoUrl: 'https://example.com/photo.jpg', + verified: true, + role: 'VENDOR', +}; +const sampleBuyer: UserInterface = { + id: buyerId, + firstName: 'buyer', + lastName: 'user', + email: 'buyer@example.com', + password: 'password', + userType: 'Buyer', + gender: 'Male', + phoneNumber: '6380996347', + photoUrl: 'https://example.com/photo.jpg', + verified: true, + role: 'BUYER', +}; + +const sampleCat = { + id: catId, + name: 'accessories', +}; + +const sampleProduct = { + id: productId, + name: 'test product', + description: 'amazing product', + images: ['photo1.jpg', 'photo2.jpg', 'photo3.jpg'], + newPrice: 200, + quantity: 10, + vendor: sampleVendor, + categories: [sampleCat], +}; +const sampleProduct2 = { + id: product2Id, + name: 'test product2', + description: 'amazing products', + images: ['photo1.jpg', 'photo2.jpg', 'photo3.jpg'], + newPrice: 200, + quantity: 10, + vendor: sampleVendor, + categories: [sampleCat], +}; + +const sampleOrder = { + id: orderId, + totalPrice: 400, + quantity: 2, + orderDate: new Date(), + buyer: sampleBuyer, + orderStatus: 'received', + address: 'Rwanda, Kigali, KK20st', +}; +const sampleOrder2 = { + id: order2Id, + totalPrice: 400, + quantity: 2, + orderDate: new Date(), + buyer: sampleBuyer, + orderStatus: 'order placed', + address: 'Rwanda, Kigali, KK20st', +}; + +const sampleOrderItem = { + id: orderItemId, + price: 200, + quantity: 2, + order: sampleOrder, + product: sampleProduct, +}; + +const sampleVendorOrder = { + id: vendorOrderId, + totalPrice: 400, + quantity: 2, + vendor: sampleVendor, + order: sampleOrder, + buyer: sampleBuyer, + orderStatus: 'pending', +}; + +const sampleVendorOrderItem = { + 'id': vendorOrderItemId, + 'price/unit': 200, + 'quantity': 2, + 'order': sampleVendorOrder, + 'product': sampleProduct, +}; + +beforeAll(async () => { + const connection = await dbConnection(); + + const categoryRepository = connection?.getRepository(Category); + await categoryRepository?.save({ ...sampleCat }); + + const userRepository = connection?.getRepository(User); + await userRepository?.save([sampleAdmin, sampleVendor, sampleVendor2, sampleBuyer]); + + const productRepository = connection?.getRepository(Product); + await productRepository?.save({ ...sampleProduct }); + + // Order Management + const orderRepository = connection?.getRepository(Order); + await orderRepository?.save([sampleOrder, sampleOrder2]); + + const orderItemRepository = connection?.getRepository(OrderItem); + await orderItemRepository?.save({ ...sampleOrderItem }); + + const vendorOrderRepository = connection?.getRepository(VendorOrders); + await vendorOrderRepository?.save({ ...sampleVendorOrder }); + + const vendorOrderItemRepository = connection?.getRepository(VendorOrderItem); + await vendorOrderItemRepository?.save({ ...sampleVendorOrderItem }); +}); + +afterAll(async () => { + await cleanDatabase(); + server.close(); +}); + +describe('Vendor Order Management', () => { + describe('Fetching vendor Order(s)', () => { + it('Should return all vendor orders', async () => { + const response = await request(app) + .get('/product/vendor/orders') + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`); + + expect(response.status).toBe(200); + expect(response.body.data.orders).toBeDefined(); + }); + + it("Should return empty array if vendor don't have any order for buyer", async () => { + const response = await request(app) + .get('/product/vendor/orders') + .set('Authorization', `Bearer ${getAccessToken(vendor2Id, sampleVendor2.email)}`); + + expect(response.status).toBe(200); + expect(response.body.data.orders).toEqual([]); + }); + + it('Should return single vendor order', async () => { + const response = await request(app) + .get(`/product/vendor/orders/${vendorOrderId}`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`); + + expect(response.status).toBe(200); + expect(response.body.data.order).toBeDefined(); + }); + + it('return 404, for non existing vendor order', async () => { + const response = await request(app) + .get(`/product/vendor/orders/${uuid()}`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`); + + expect(response.status).toBe(404); + expect(response.body.message).toBe('Order Not Found.'); + }); + + it('return 400, for invalid vendor order id ', async () => { + const response = await request(app) + .get(`/product/vendor/orders/32df3`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`); + + expect(response.status).toBe(400); + expect(response.body.message).toBe(`invalid input syntax for type uuid: "32df3"`); + }); + }); + describe('Updating vendor order', () => { + it('should update the order', async () => { + const response = await request(app) + .put(`/product/vendor/orders/${vendorOrderId}`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`) + .send({ + orderStatus: 'delivered', + }); + + expect(response.statusCode).toBe(200); + }); + it('should not update if orderStatus in not among defined ones', async () => { + const response = await request(app) + .put(`/product/vendor/orders/${vendorOrderId}`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`) + .send({ + orderStatus: 'fakeOrderStatus', + }); + + expect(response.statusCode).toBe(400); + expect(response.body.message).toBe('Please provide one of defined statuses.'); + }); + it('should not update, return 404 for non existing vendor order', async () => { + const response = await request(app) + .put(`/product/vendor/orders/${uuid()}`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`) + .send({ + orderStatus: 'is-accepted', + }); + + expect(response.statusCode).toBe(404); + expect(response.body.message).toBe('Order Not Found.'); + }); + it('should not update, if the order has already been cancelled or completed', async () => { + const response = await request(app) + .put(`/product/vendor/orders/${vendorOrderId}`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`) + .send({ + orderStatus: 'is-accepted', + }); + + expect(response.statusCode).toBe(409); + }); + it('return 400, for invalid vendor order id ', async () => { + const response = await request(app) + .put(`/product/vendor/orders/32df3`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`) + .send({ + orderStatus: 'is-accepted', + }); + + expect(response.status).toBe(400); + expect(response.body.message).toBe(`invalid input syntax for type uuid: "32df3"`); + }); + }); +}); + +describe('Admin Order Management', () => { + describe('Fetching buyer and vendor Order(s)', () => { + it("Should return all orders with it's buyer and related vendors", async () => { + const response = await request(app) + .get('/product/admin/orders') + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.status).toBe(200); + expect(response.body.data.orders).toBeDefined(); + }); + + it('Should return single order details', async () => { + const response = await request(app) + .get(`/product/admin/orders/${orderId}`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.status).toBe(200); + expect(response.body.data.order).toBeDefined(); + }); + + it('return 404, for non existing order', async () => { + const response = await request(app) + .get(`/product/admin/orders/${uuid()}`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.status).toBe(404); + expect(response.body.message).toBe('Order Not Found.'); + }); + + it('return 400, for invalid order id ', async () => { + const response = await request(app) + .get(`/product/admin/orders/32df3`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.status).toBe(400); + expect(response.body.message).toBe(`invalid input syntax for type uuid: "32df3"`); + }); + }); + describe('Updating buyer and vendor order', () => { + it('should not update, return 404 for non existing order', async () => { + const response = await request(app) + .put(`/product/admin/orders/${uuid()}`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.statusCode).toBe(404); + expect(response.body.message).toBe('Order Not Found.'); + }); + it('should update the order', async () => { + const response = await request(app) + .put(`/product/admin/orders/${orderId}`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.statusCode).toBe(200); + expect(response.body.data.order).toBeDefined(); + }); + it('should not update if it has already been completed(closed)', async () => { + const response = await request(app) + .put(`/product/admin/orders/${orderId}`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.statusCode).toBe(409); + expect(response.body.message).toBe('The order has already been completed.'); + }); + + it('should not update, if the order has not been marked as received by buyer', async () => { + const response = await request(app) + .put(`/product/admin/orders/${order2Id}`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.statusCode).toBe(409); + expect(response.body.message).toBe('Order closure failed: The buyer has not received the item yet.'); + }); + + it('return 400, for invalid order id ', async () => { + const response = await request(app) + .put(`/product/admin/orders/32df3`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.status).toBe(400); + expect(response.body.message).toBe(`invalid input syntax for type uuid: "32df3"`); + }); + }); +}); diff --git a/src/__test__/productStatus.test.ts b/src/__test__/productStatus.test.ts index 6d6df6a..8e8b42a 100644 --- a/src/__test__/productStatus.test.ts +++ b/src/__test__/productStatus.test.ts @@ -144,7 +144,7 @@ beforeAll(async () => { }); afterAll(async () => { - await cleanDatabase() + await cleanDatabase(); server.close(); }); @@ -222,22 +222,16 @@ describe('Vendor product availability status management tests', () => { }); }); - describe('search product by name availability tests', () => { it('Should search product by name', async () => { - const response = await request(app) - .get(`/product/search?name=testingmkknkkjiproduct4`) + const response = await request(app).get(`/product/search?name=testingmkknkkjiproduct4`); expect(response.body.data).toBeDefined; }, 10000); it('should return empty array if there is product is not found in the database', async () => { - const response = await request(app) - .put(`/product/search?name=home`) - + const response = await request(app).put(`/product/search?name=home`); expect(response.statusCode).toBe(401); expect(response.body.data).toBeUndefined; }); - - }); - +}); diff --git a/src/__test__/roleCheck.test.ts b/src/__test__/roleCheck.test.ts index ada2271..32df044 100644 --- a/src/__test__/roleCheck.test.ts +++ b/src/__test__/roleCheck.test.ts @@ -35,7 +35,7 @@ beforeAll(async () => { }); afterAll(async () => { - await cleanDatabase() + await cleanDatabase(); }); describe('hasRole MiddleWare Test', () => { diff --git a/src/__test__/route.test.ts b/src/__test__/route.test.ts index 721f763..ac704b5 100644 --- a/src/__test__/route.test.ts +++ b/src/__test__/route.test.ts @@ -11,8 +11,7 @@ beforeAll(async () => { jest.setTimeout(20000); afterAll(async () => { - await cleanDatabase() - + await cleanDatabase(); server.close(); }); diff --git a/src/__test__/test-assets/DatabaseCleanup.ts b/src/__test__/test-assets/DatabaseCleanup.ts index ec40ee6..3674dfb 100644 --- a/src/__test__/test-assets/DatabaseCleanup.ts +++ b/src/__test__/test-assets/DatabaseCleanup.ts @@ -1,16 +1,17 @@ - import { Transaction } from '../../entities/transaction'; -import { Cart } from "../../entities/Cart"; -import { CartItem } from "../../entities/CartItem"; -import { Order } from "../../entities/Order"; -import { OrderItem } from "../../entities/OrderItem"; -import { wishList } from "../../entities/wishList"; +import { Cart } from '../../entities/Cart'; +import { CartItem } from '../../entities/CartItem'; +import { Order } from '../../entities/Order'; +import { OrderItem } from '../../entities/OrderItem'; +import { wishList } from '../../entities/wishList'; import { getConnection } from 'typeorm'; import { Product } from '../../entities/Product'; import { Category } from '../../entities/Category'; import { Coupon } from '../../entities/coupon'; import { User } from '../../entities/User'; import { server } from '../..'; +import { VendorOrderItem } from '../../entities/VendorOrderItem'; +import { VendorOrders } from '../../entities/vendorOrders'; export const cleanDatabase = async () => { const connection = getConnection(); @@ -18,6 +19,8 @@ export const cleanDatabase = async () => { // Delete from child tables first await connection.getRepository(Transaction).delete({}); await connection.getRepository(Coupon).delete({}); + await connection.getRepository(VendorOrderItem).delete({}); + await connection.getRepository(VendorOrders).delete({}); await connection.getRepository(OrderItem).delete({}); await connection.getRepository(Order).delete({}); await connection.getRepository(CartItem).delete({}); @@ -37,12 +40,11 @@ export const cleanDatabase = async () => { await connection.getRepository(User).delete({}); await connection.close(); - server.close(); }; -// Execute the clean-up function -cleanDatabase().then(() => { - console.log('Database cleaned'); -}).catch(error => { - console.error('Error cleaning database:', error); -}); +// // Execute the clean-up function +// cleanDatabase().then(() => { +// console.log('Database cleaned'); +// }).catch(error => { +// console.error('Error cleaning database:', error); +// }); diff --git a/src/__test__/userServices.test.ts b/src/__test__/userServices.test.ts index b4e87f9..29a2e7c 100644 --- a/src/__test__/userServices.test.ts +++ b/src/__test__/userServices.test.ts @@ -1,6 +1,6 @@ import request from 'supertest'; import { app, server } from '../index'; -import { createConnection, getConnection, getConnectionOptions, getRepository } from 'typeorm'; +import { createConnection, getRepository } from 'typeorm'; import { User } from '../entities/User'; import { cleanDatabase } from './test-assets/DatabaseCleanup'; @@ -9,7 +9,7 @@ beforeAll(async () => { }); afterAll(async () => { - await cleanDatabase() + await cleanDatabase(); server.close(); }); @@ -227,4 +227,4 @@ describe('start2FAProcess', () => { expect(res.status).toBe(404); expect(res.body).toEqual({ status: 'error', message: 'Incorrect email or password' }); }, 10000); -}); \ No newline at end of file +}); diff --git a/src/__test__/userStatus.test.ts b/src/__test__/userStatus.test.ts index 132134f..69e892a 100644 --- a/src/__test__/userStatus.test.ts +++ b/src/__test__/userStatus.test.ts @@ -36,7 +36,7 @@ beforeAll(async () => { }); afterAll(async () => { - await cleanDatabase() + await cleanDatabase(); server.close(); }); diff --git a/src/__test__/vendorProduct.test.ts b/src/__test__/vendorProduct.test.ts index f90d80d..d8fc0a5 100644 --- a/src/__test__/vendorProduct.test.ts +++ b/src/__test__/vendorProduct.test.ts @@ -110,7 +110,7 @@ beforeAll(async () => { }); afterAll(async () => { - await cleanDatabase() + await cleanDatabase(); server.close(); }); diff --git a/src/__test__/wishList.test.ts b/src/__test__/wishList.test.ts index aac072d..6658853 100644 --- a/src/__test__/wishList.test.ts +++ b/src/__test__/wishList.test.ts @@ -65,7 +65,7 @@ beforeAll(async () => { }); afterAll(async () => { - await cleanDatabase() + await cleanDatabase(); server.close(); }); const data1 = { diff --git a/src/controllers/adminOrdercontroller.ts b/src/controllers/adminOrdercontroller.ts new file mode 100644 index 0000000..388220d --- /dev/null +++ b/src/controllers/adminOrdercontroller.ts @@ -0,0 +1,18 @@ +import { Request, Response } from 'express'; +import { + getSingleBuyerVendorOrderService, + getBuyerVendorOrdersService, + updateBuyerVendorOrderService, +} from '../services'; + +export const getBuyerVendorOrders = async (req: Request, res: Response) => { + await getBuyerVendorOrdersService(req, res); +}; + +export const getSingleBuyerVendorOrder = async (req: Request, res: Response) => { + await getSingleBuyerVendorOrderService(req, res); +}; + +export const updateBuyerVendorOrder = async (req: Request, res: Response) => { + await updateBuyerVendorOrderService(req, res); +}; diff --git a/src/controllers/couponController.ts b/src/controllers/couponController.ts index dd7e19f..e5a6804 100644 --- a/src/controllers/couponController.ts +++ b/src/controllers/couponController.ts @@ -4,7 +4,7 @@ import { updateCouponService } from '../services/couponServices/updateService'; import { deleteCouponService } from '../services/couponServices/deleteCoupon'; import { accessAllCouponService } from '../services/couponServices/accessAllCoupon'; import { readCouponService } from '../services/couponServices/readCoupon'; -import { buyerApplyCouponService } from '../services/couponServices/buyerApplyCoupon' +import { buyerApplyCouponService } from '../services/couponServices/buyerApplyCoupon'; export const createCoupon = async (req: Request, res: Response) => { await createCouponService(req, res); @@ -28,4 +28,4 @@ export const readCoupon = async (req: Request, res: Response) => { export const buyerApplyCoupon = async (req: Request, res: Response) => { await buyerApplyCouponService(req, res); -}; \ No newline at end of file +}; diff --git a/src/controllers/index.ts b/src/controllers/index.ts index 3cbb7dc..70dea3b 100644 --- a/src/controllers/index.ts +++ b/src/controllers/index.ts @@ -1,3 +1,5 @@ export * from './authController'; export * from './productController'; -export * from './orderController'; \ No newline at end of file +export * from './orderController'; +export * from './vendorOrderController'; +export * from './adminOrdercontroller'; diff --git a/src/controllers/orderController.ts b/src/controllers/orderController.ts index 5a5db97..d4ac5fc 100644 --- a/src/controllers/orderController.ts +++ b/src/controllers/orderController.ts @@ -15,4 +15,4 @@ export const updateOrder = async (req: Request, res: Response) => { }; export const getOrdersHistory = async (req: Request, res: Response) => { await getTransactionHistoryService(req, res); -}; \ No newline at end of file +}; diff --git a/src/controllers/productController.ts b/src/controllers/productController.ts index 11caddd..1cd895a 100644 --- a/src/controllers/productController.ts +++ b/src/controllers/productController.ts @@ -1,26 +1,17 @@ import { Request, Response } from 'express'; import { - createProductService, - updateProductService, - - removeProductImageService, - + removeProductImageService, readProductService, - readProductsService, - + readProductsService, deleteProductService, - getRecommendedProductsService, productStatusServices, viewSingleProduct, - searchProductService - -, - listAllProductsService} -from '../services'; - + searchProductService, + listAllProductsService, +} from '../services'; export const readProduct = async (req: Request, res: Response) => { await readProductService(req, res); @@ -50,10 +41,10 @@ export const getRecommendedProducts = async (req: Request, res: Response) => { await getRecommendedProductsService(req, res); }; - export const listAllProducts = async (req: Request, res: Response) => { await listAllProductsService(req, res); -};export const productStatus = async (req: Request, res: Response) => { +}; +export const productStatus = async (req: Request, res: Response) => { await productStatusServices(req, res); }; export const singleProduct = async (req: Request, res: Response) => { diff --git a/src/controllers/vendorOrderController.ts b/src/controllers/vendorOrderController.ts new file mode 100644 index 0000000..955b01c --- /dev/null +++ b/src/controllers/vendorOrderController.ts @@ -0,0 +1,14 @@ +import { Request, Response } from 'express'; +import { getVendorOrdersService, getSingleVendorOrderService, updateVendorOrderService } from '../services'; + +export const getVendorOrders = async (req: Request, res: Response) => { + await getVendorOrdersService(req, res); +}; + +export const getSingleVendorOrder = async (req: Request, res: Response) => { + await getSingleVendorOrderService(req, res); +}; + +export const updateVendorOrder = async (req: Request, res: Response) => { + await updateVendorOrderService(req, res); +}; diff --git a/src/controllers/wishListController.ts b/src/controllers/wishListController.ts index e0cd1bd..23fa03f 100644 --- a/src/controllers/wishListController.ts +++ b/src/controllers/wishListController.ts @@ -1,23 +1,18 @@ import { Request, Response } from 'express'; -import{ - addProductService, - getProductsService, - removeProductService, - clearAllProductService -} from '../services' +import { addProductService, getProductsService, removeProductService, clearAllProductService } from '../services'; export const wishlistAddProduct = async (req: Request, res: Response) => { - await addProductService(req, res); - }; + await addProductService(req, res); +}; - export const wishlistRemoveProduct = async (req: Request, res:Response) => { - await removeProductService(req, res); - } +export const wishlistRemoveProduct = async (req: Request, res: Response) => { + await removeProductService(req, res); +}; - export const wishlistGetProducts = async (req: Request, res:Response) => { - await getProductsService(req, res); - } +export const wishlistGetProducts = async (req: Request, res: Response) => { + await getProductsService(req, res); +}; - export const wishlistClearAllProducts = async (req: Request, res:Response) => { - await clearAllProductService(req, res); - } \ No newline at end of file +export const wishlistClearAllProducts = async (req: Request, res: Response) => { + await clearAllProductService(req, res); +}; diff --git a/src/docs/adminOrderManagement.yml b/src/docs/adminOrderManagement.yml new file mode 100644 index 0000000..e8d6ed6 --- /dev/null +++ b/src/docs/adminOrderManagement.yml @@ -0,0 +1,80 @@ +/product/admin/orders: + get: + tags: + - Admin Order Manangement + summary: Fetches all buyer and vendor orders + description: Return all order including details for buyer and vendors of products in that order + security: + - bearerAuth: [] + responses: + '200': + description: Return all order + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '500': + description: Internal server error + +/product/admin/orders/{id}: + get: + tags: + - Admin Order Manangement + summary: Fetch details for single buyer and vendor order + description: + Fetch details for single order using buyer id, if successful return order details with it's corresponding vendor + security: + - bearerAuth: [] + parameters: + - in: path + name: id + schema: + type: string + required: true + description: The id of a buyer order + responses: + '200': + description: Order details retrieved successfully + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Order not found + '500': + description: Internal server error + put: + tags: + - Admin Order Manangement + summary: Updates order status for both buyer and vendor order + description: Updates orderStatus field of order, if successful returns updated order. + security: + - bearerAuth: [] + parameters: + - in: path + name: id + schema: + type: string + required: true + description: The id of a buyer order + consumes: + - application/json + responses: + '200': + description: Order was successfully updated, return updated order + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Order not found + '409': + description: Order can not be updated because (it has already been completed(close), delivered, cancelled) + '500': + description: Internal server error diff --git a/src/docs/couponDocs.yml b/src/docs/couponDocs.yml index fb0a49a..fefa829 100644 --- a/src/docs/couponDocs.yml +++ b/src/docs/couponDocs.yml @@ -35,7 +35,7 @@ description: The code of the coupon responses: '200': - description: Return info for the coupon + description: Return info for the coupon '400': description: Bad Request (syntax error, incorrect input format, etc..) '401': diff --git a/src/docs/vendorOrderManagement.yml b/src/docs/vendorOrderManagement.yml new file mode 100644 index 0000000..5873717 --- /dev/null +++ b/src/docs/vendorOrderManagement.yml @@ -0,0 +1,93 @@ +/product/vendor/orders: + get: + tags: + - Vendor Order Manangement + summary: Fetches all vendor orders + description: Return all order for authenticated vendor + security: + - bearerAuth: [] + responses: + '200': + description: Return all order for vendor requested from buyer + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '500': + description: Internal server error + +/product/vendor/orders/{id}: + get: + tags: + - Vendor Order Manangement + summary: Fetch details for single vendor order + description: + Fetch details for single order for authenticated vendor, order that include only his/her product which a buyer has + requested in his order. + security: + - bearerAuth: [] + parameters: + - in: path + name: id + schema: + type: string + required: true + description: The id of a vendor order + responses: + '200': + description: Order details retrieved successfully + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Order not found + '500': + description: Internal server error + put: + tags: + - Vendor Order Manangement + summary: Updates order status for vendor order + description: + Updates orderStatus field of vendor order for authenticated vendor, it order that include only his/her product + which a buyer has request in his order. + security: + - bearerAuth: [] + parameters: + - in: path + name: id + schema: + type: string + required: true + description: The id of a vendor order + consumes: + - application/json + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + orderStatus: + type: string + example: "'is-accepted', 'in-transit', 'cancelled', 'delivered'" + responses: + '200': + description: Order was successfully updated, return updated order + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Order not found + '409': + description: Order can not be updated because (it has already been completed(close), delivered, cancelled) + '500': + description: Internal server error diff --git a/src/docs/vendorProduct.yml b/src/docs/vendorProduct.yml index 937b097..3830bf4 100644 --- a/src/docs/vendorProduct.yml +++ b/src/docs/vendorProduct.yml @@ -35,7 +35,7 @@ description: The id of product responses: '200': - description: Return info for the product + description: Return info for the product '400': description: Bad Request (syntax error, incorrect input format, etc..) '401': @@ -59,7 +59,7 @@ requestBody: required: true content: - application/json: + application/json: schema: type: object properties: @@ -75,10 +75,10 @@ type: file categories: oneOf: - - type: string - - type: array - items: - type: string + - type: string + - type: array + items: + type: string example: "'category' or ['category1', 'category2', ...]" expirationDate: type: string @@ -159,7 +159,7 @@ description: Product not found '500': description: Internal server error - + /product/images/{id}: delete: tags: diff --git a/src/docs/wishListDocs.yml b/src/docs/wishListDocs.yml index df3c72c..7f705f7 100644 --- a/src/docs/wishListDocs.yml +++ b/src/docs/wishListDocs.yml @@ -94,4 +94,4 @@ '403': description: Forbidden (Unauthorized action) '500': - description: Internal server error \ No newline at end of file + description: Internal server error diff --git a/src/entities/Cart.ts b/src/entities/Cart.ts index 0ba44a6..fda1e15 100644 --- a/src/entities/Cart.ts +++ b/src/entities/Cart.ts @@ -36,7 +36,7 @@ export class Cart { @UpdateDateColumn() updatedAt!: Date; - updateTotal(): void { + updateTotal (): void { if (this.items) { let total: number = 0; for (let i = 0; i < this.items.length; i++) { diff --git a/src/entities/CartItem.ts b/src/entities/CartItem.ts index 107170c..d651adf 100644 --- a/src/entities/CartItem.ts +++ b/src/entities/CartItem.ts @@ -47,7 +47,7 @@ export class CartItem { @BeforeInsert() @BeforeUpdate() - updateTotal(): void { + updateTotal (): void { this.total = this.newPrice * this.quantity; } } diff --git a/src/entities/Order.ts b/src/entities/Order.ts index 49965a0..47649a7 100644 --- a/src/entities/Order.ts +++ b/src/entities/Order.ts @@ -1,10 +1,17 @@ -import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToMany, CreateDateColumn, UpdateDateColumn } from 'typeorm'; +import { + Entity, + PrimaryGeneratedColumn, + Column, + ManyToOne, + OneToMany, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm'; import { IsNotEmpty, IsNumber, IsDate, IsIn } from 'class-validator'; import { User } from './User'; import { OrderItem } from './OrderItem'; import { Transaction } from './transaction'; - @Entity() export class Order { @PrimaryGeneratedColumn('uuid') @@ -24,11 +31,20 @@ export class Order { @IsNumber() totalPrice!: number; - @OneToMany(() => Transaction, (transaction) => transaction.order) + @OneToMany(() => Transaction, transaction => transaction.order) transactions!: Transaction[]; @Column({ default: 'order placed' }) @IsNotEmpty() - @IsIn(['order placed', 'cancelled', 'awaiting shipment', 'in transit', 'delivered', 'received', 'returned']) + @IsIn([ + 'order placed', + 'cancelled', + 'awaiting shipment', + 'in transit', + 'delivered', + 'received', + 'returned', + 'completed', + ]) orderStatus!: string; @Column('int') diff --git a/src/entities/Product.ts b/src/entities/Product.ts index 2b39493..e144a04 100644 --- a/src/entities/Product.ts +++ b/src/entities/Product.ts @@ -18,11 +18,12 @@ import { Category } from './Category'; import { Order } from './Order'; import { Coupon } from './coupon'; import { OrderItem } from './OrderItem'; +import { VendorOrderItem } from './VendorOrderItem'; @Entity() @Unique(['id']) export class Product { - static query() { + static query () { throw new Error('Method not implemented.'); } @PrimaryGeneratedColumn('uuid') @@ -36,6 +37,9 @@ export class Product { @OneToMany(() => OrderItem, orderItem => orderItem.product) orderItems!: OrderItem[]; + @OneToMany(() => VendorOrderItem, vendorOrderItems => vendorOrderItems.product) + vendorOrderItems!: VendorOrderItem[]; + @OneToOne(() => Coupon, (coupons: any) => coupons.product) @JoinColumn() coupons?: Coupon; diff --git a/src/entities/User.ts b/src/entities/User.ts index fb45fe9..eebd104 100644 --- a/src/entities/User.ts +++ b/src/entities/User.ts @@ -99,7 +99,7 @@ export class User { @OneToMany(() => Order, (order: any) => order.buyer) orders!: Order[]; - @OneToMany(() => Transaction, (transaction) => transaction.user) + @OneToMany(() => Transaction, transaction => transaction.user) transactions!: Transaction[]; @CreateDateColumn() @@ -112,7 +112,7 @@ export class User { accountBalance!: number; @BeforeInsert() - setRole(): void { + setRole (): void { this.role = this.userType === 'Vendor' ? roles.vendor : roles.buyer; } } diff --git a/src/entities/VendorOrderItem.ts b/src/entities/VendorOrderItem.ts new file mode 100644 index 0000000..9137f6d --- /dev/null +++ b/src/entities/VendorOrderItem.ts @@ -0,0 +1,30 @@ +import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'; +import { IsNotEmpty, IsNumber } from 'class-validator'; +import { Order } from './Order'; +import { Product } from './Product'; +import { VendorOrders } from './vendorOrders'; + +@Entity() +export class VendorOrderItem { + @PrimaryGeneratedColumn('uuid') + @IsNotEmpty() + 'id'!: string; + + @ManyToOne(() => VendorOrders, order => order.vendorOrderItems) + @IsNotEmpty() + 'order'!: VendorOrders; + + @ManyToOne(() => Product, product => product.vendorOrderItems) + @IsNotEmpty() + 'product'!: Product; + + @Column('decimal') + @IsNotEmpty() + @IsNumber() + 'price/unit'!: number; + + @Column('int') + @IsNotEmpty() + @IsNumber() + 'quantity'!: number; +} diff --git a/src/entities/transaction.ts b/src/entities/transaction.ts index d475812..0f7b0ea 100644 --- a/src/entities/transaction.ts +++ b/src/entities/transaction.ts @@ -58,4 +58,4 @@ export class Transaction { @UpdateDateColumn() updatedAt!: Date; -} \ No newline at end of file +} diff --git a/src/entities/vendorOrders.ts b/src/entities/vendorOrders.ts new file mode 100644 index 0000000..38269e6 --- /dev/null +++ b/src/entities/vendorOrders.ts @@ -0,0 +1,49 @@ +import { + Entity, + PrimaryGeneratedColumn, + Column, + ManyToOne, + OneToMany, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm'; +import { IsNotEmpty, IsNumber, IsDate, IsIn, isNotEmpty } from 'class-validator'; +import { User } from './User'; +import { OrderItem } from './OrderItem'; +import { Transaction } from './transaction'; +import { Order } from './Order'; +import { VendorOrderItem } from './VendorOrderItem'; + +@Entity() +export class VendorOrders { + @PrimaryGeneratedColumn('uuid') + @IsNotEmpty() + id!: string; + + @ManyToOne(() => User) + @IsNotEmpty() + vendor!: User; + + @OneToMany(() => VendorOrderItem, vendorOrderItems => vendorOrderItems.order, { cascade: true }) + @IsNotEmpty() + vendorOrderItems!: VendorOrderItem[]; + + @ManyToOne(() => Order) + @IsNotEmpty() + order!: Order; + + @Column('decimal') + @IsNotEmpty() + @IsNumber() + totalPrice!: number; + + @Column({ default: 'pending' }) + @IsIn(['pending', 'is-accepted', 'in-transit', 'cancelled', 'delivered', 'completed']) + orderStatus!: string; + + @CreateDateColumn() + createdAt!: Date; + + @UpdateDateColumn() + updatedAt!: Date; +} diff --git a/src/entities/wishList.ts b/src/entities/wishList.ts index 69dbebd..7f74023 100644 --- a/src/entities/wishList.ts +++ b/src/entities/wishList.ts @@ -1,10 +1,19 @@ -import { Entity, PrimaryGeneratedColumn, BaseEntity,Column, Unique, ManyToOne, CreateDateColumn, UpdateDateColumn,} from "typeorm"; +import { + Entity, + PrimaryGeneratedColumn, + BaseEntity, + Column, + Unique, + ManyToOne, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm'; import { IsNotEmpty, IsString } from 'class-validator'; import { User } from './User'; -@Entity("wishlist") +@Entity('wishlist') @Unique(['id']) -export class wishList extends BaseEntity{ +export class wishList extends BaseEntity { @PrimaryGeneratedColumn() @IsNotEmpty() id!: number; @@ -23,4 +32,4 @@ export class wishList extends BaseEntity{ @UpdateDateColumn() updatedAt!: Date; -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index 07efd39..d689c27 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,21 +5,27 @@ import router from './routes'; import { addDocumentation } from './startups/docs'; import 'reflect-metadata'; import cookieParser from 'cookie-parser'; -import session from "express-session"; +import session from 'express-session'; import passport from 'passport'; import { CustomError, errorHandler } from './middlewares/errorHandler'; import morgan from 'morgan'; import { dbConnection } from './startups/dbConnection'; + +import { Server } from 'socket.io'; +import { init as initSocketIO } from './utils/socket'; + dotenv.config(); export const app = express(); const port = process.env.PORT || 8000; -app.use(session({ - secret: 'keyboard cat' -})) -app.use(passport.initialize()) -app.use(passport.session()) +app.use( + session({ + secret: 'keyboard cat', + }) +); +app.use(passport.initialize()); +app.use(passport.session()); app.use(express.json()); app.use(cookieParser()); app.use(cors({ origin: '*' })); @@ -43,3 +49,14 @@ app.use(morgan(morganFormat)); export const server = app.listen(port, () => { console.log(`[server]: Server is running at http://localhost:${port}`); }); + +// Socket.IO setup +const io = initSocketIO(server); + +io.on('connection', socket => { + console.log('Client connected'); + + socket.on('disconnect', () => { + console.log('Client disconnected'); + }); +}); diff --git a/src/routes/ProductRoutes.ts b/src/routes/ProductRoutes.ts index ce146ec..614eaaf 100644 --- a/src/routes/ProductRoutes.ts +++ b/src/routes/ProductRoutes.ts @@ -18,8 +18,15 @@ import { createOrder, getOrders, updateOrder, - getOrdersHistory + getOrdersHistory, + getSingleVendorOrder, + getVendorOrders, + updateVendorOrder, + getBuyerVendorOrders, + getSingleBuyerVendorOrder, + updateBuyerVendorOrder, } from '../controllers'; + const router = Router(); router.get('/all', listAllProducts); router.get('/recommended', authMiddleware as RequestHandler, hasRole('BUYER'), getRecommendedProducts); @@ -32,9 +39,20 @@ router.put('/:id', authMiddleware as RequestHandler, hasRole('VENDOR'), upload.a router.delete('/images/:id', authMiddleware as RequestHandler, hasRole('VENDOR'), removeProductImage); router.delete('/:id', authMiddleware as RequestHandler, hasRole('VENDOR'), deleteProduct); router.put('/availability/:id', authMiddleware as RequestHandler, hasRole('VENDOR'), productStatus); + router.post('/orders', authMiddleware as RequestHandler, hasRole('BUYER'), createOrder); router.get('/client/orders', authMiddleware as RequestHandler, hasRole('BUYER'), getOrders); router.put('/client/orders/:orderId', authMiddleware as RequestHandler, hasRole('BUYER'), updateOrder); router.get('/orders/history', authMiddleware as RequestHandler, hasRole('BUYER'), getOrdersHistory); +// Vendor order management +router.get('/vendor/orders', authMiddleware as RequestHandler, hasRole('VENDOR'), getVendorOrders); +router.get('/vendor/orders/:id', authMiddleware as RequestHandler, hasRole('VENDOR'), getSingleVendorOrder); +router.put('/vendor/orders/:id', authMiddleware as RequestHandler, hasRole('VENDOR'), updateVendorOrder); + +// Admin order management +router.get('/admin/orders', authMiddleware as RequestHandler, hasRole('ADMIN'), getBuyerVendorOrders); +router.get('/admin/orders/:id', authMiddleware as RequestHandler, hasRole('ADMIN'), getSingleBuyerVendorOrder); +router.put('/admin/orders/:id', authMiddleware as RequestHandler, hasRole('ADMIN'), updateBuyerVendorOrder); + export default router; diff --git a/src/routes/UserRoutes.ts b/src/routes/UserRoutes.ts index 50bb4ca..79b0551 100644 --- a/src/routes/UserRoutes.ts +++ b/src/routes/UserRoutes.ts @@ -1,7 +1,7 @@ import { Router } from 'express'; import { responseError } from '../utils/response.utils'; import { UserInterface } from '../entities/User'; -import jwt from 'jsonwebtoken' +import jwt from 'jsonwebtoken'; import { disable2FA, enable2FA, @@ -19,7 +19,7 @@ import { activateUser, disactivateUser, userProfileUpdate } from '../controllers import { hasRole } from '../middlewares/roleCheck'; import { isTokenValide } from '../middlewares/isValid'; import passport from 'passport'; -import "../utils/auth"; +import '../utils/auth'; const router = Router(); router.post('/register', userRegistration); @@ -37,35 +37,36 @@ router.post('/password/reset/link', sendPasswordResetLink); router.put('/update', userProfileUpdate); router.get('/google-auth', passport.authenticate('google', { scope: ['profile', 'email'] })); -router.get("/auth/google/callback", - passport.authenticate("google", { - successRedirect: "/user/login/success", - failureRedirect: "/user/login/failed" +router.get( + '/auth/google/callback', + passport.authenticate('google', { + successRedirect: '/user/login/success', + failureRedirect: '/user/login/failed', }) ); -router.get("/login/success", async (req, res) => { +router.get('/login/success', async (req, res) => { const user = req.user as UserInterface; - if(!user){ - responseError(res, 404, 'user not found') + if (!user) { + responseError(res, 404, 'user not found'); } const payload = { id: user?.id, email: user?.email, - role: user?.role - } - const token = jwt.sign(payload, process.env.JWT_SECRET as string,{expiresIn: '24h'}) + role: user?.role, + }; + const token = jwt.sign(payload, process.env.JWT_SECRET as string, { expiresIn: '24h' }); res.status(200).json({ status: 'success', - data:{ - token: token, - message: "Login success" - } - }) + data: { + token: token, + message: 'Login success', + }, + }); }); -router.get("/login/failed", async (req, res) => { +router.get('/login/failed', async (req, res) => { res.status(401).json({ status: false, - message: "Login failed" + message: 'Login failed', }); }); diff --git a/src/routes/couponRoutes.ts b/src/routes/couponRoutes.ts index c315ab8..3378fbe 100644 --- a/src/routes/couponRoutes.ts +++ b/src/routes/couponRoutes.ts @@ -1,5 +1,12 @@ import { RequestHandler, Router } from 'express'; -import { createCoupon, updateCoupon, accessAllCoupon, readCoupon, deleteCoupon, buyerApplyCoupon } from '../controllers/couponController'; +import { + createCoupon, + updateCoupon, + accessAllCoupon, + readCoupon, + deleteCoupon, + buyerApplyCoupon, +} from '../controllers/couponController'; import { hasRole } from '../middlewares/roleCheck'; import { authMiddleware } from '../middlewares/verifyToken'; @@ -10,6 +17,6 @@ router.put('/vendor/:id/update-coupon/:code', authMiddleware as RequestHandler, router.get('/vendor/:id/checkout/:code', authMiddleware as RequestHandler, hasRole('VENDOR'), readCoupon); router.get('/vendor/:id/access-coupons', authMiddleware as RequestHandler, hasRole('VENDOR'), accessAllCoupon); router.delete('/vendor/:id/checkout/delete', authMiddleware as RequestHandler, hasRole('VENDOR'), deleteCoupon); -router.post('/apply', authMiddleware as RequestHandler, hasRole('BUYER'),buyerApplyCoupon); +router.post('/apply', authMiddleware as RequestHandler, hasRole('BUYER'), buyerApplyCoupon); -export default router; \ No newline at end of file +export default router; diff --git a/src/routes/index.ts b/src/routes/index.ts index cddc08a..6f632d6 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -3,7 +3,7 @@ import { responseSuccess } from '../utils/response.utils'; import userRoutes from './UserRoutes'; import productRoutes from './ProductRoutes'; import wishListRoutes from './wishListRoute'; -import couponRoute from './couponRoutes';; +import couponRoute from './couponRoutes'; import cartRoutes from './CartRoutes'; const router = Router(); @@ -14,7 +14,7 @@ router.get('/', (req: Request, res: Response) => { router.use('/user', userRoutes); router.use('/product', productRoutes); -router.use('/wish-list', wishListRoutes); +router.use('/wish-list', wishListRoutes); router.use('/cart', cartRoutes); router.use('/coupons', couponRoute); diff --git a/src/routes/wishListRoute.ts b/src/routes/wishListRoute.ts index d5ac6fb..ea96e40 100644 --- a/src/routes/wishListRoute.ts +++ b/src/routes/wishListRoute.ts @@ -2,13 +2,30 @@ import { RequestHandler, Router } from 'express'; import { authMiddleware } from '../middlewares/verifyToken'; import { hasRole } from '../middlewares'; import { checkUserStatus } from '../middlewares/isAllowed'; -import { wishlistAddProduct,wishlistRemoveProduct,wishlistGetProducts,wishlistClearAllProducts } from '../controllers/wishListController'; +import { + wishlistAddProduct, + wishlistRemoveProduct, + wishlistGetProducts, + wishlistClearAllProducts, +} from '../controllers/wishListController'; const router = Router(); router.post('/add/:id', authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'), wishlistAddProduct); -router.get('/',authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'),wishlistGetProducts); -router.delete('/delete/:id',authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'),wishlistRemoveProduct); -router.delete('/clearAll',authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'),wishlistClearAllProducts); +router.get('/', authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'), wishlistGetProducts); +router.delete( + '/delete/:id', + authMiddleware as RequestHandler, + checkUserStatus, + hasRole('BUYER'), + wishlistRemoveProduct +); +router.delete( + '/clearAll', + authMiddleware as RequestHandler, + checkUserStatus, + hasRole('BUYER'), + wishlistClearAllProducts +); -export default router; \ No newline at end of file +export default router; diff --git a/src/services/adminOrderServices/readOrder.ts b/src/services/adminOrderServices/readOrder.ts new file mode 100644 index 0000000..4bb20f0 --- /dev/null +++ b/src/services/adminOrderServices/readOrder.ts @@ -0,0 +1,158 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { responseSuccess, responseError } from '../../utils/response.utils'; +import { VendorOrderItem } from '../../entities/VendorOrderItem'; +import { VendorOrders } from '../../entities/vendorOrders'; +import { Order } from '../../entities/Order'; + +export const getBuyerVendorOrdersService = async (req: Request, res: Response) => { + try { + const vendorOrderRepository = getRepository(VendorOrders); + const orderRepository = getRepository(Order); + + const orders = await orderRepository.find({ + relations: ['buyer', 'orderItems'], + order: { + createdAt: 'DESC', // Order by creation date, most recent first + }, + }); + + if (!orders.length) { + return responseError(res, 200, `There is no pending orders from buyer`, { orders: [] }); + } + + const sanitizedOrdersResponse = []; + + for (const order of orders) { + const vendorOrders = await vendorOrderRepository.find({ + where: { + order: { + id: order.id, + }, + }, + relations: ['vendor', 'vendorOrderItems', 'vendorOrderItems.product'], + order: { + createdAt: 'DESC', // Order by creation date, most recent first + }, + }); + + sanitizedOrdersResponse.push({ + id: order.id, + totalPrice: order.totalPrice, + totalProducts: order.orderItems.length, + orderStatus: order.orderStatus, + address: order.address, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + buyer: { + id: order.buyer.id, + firstName: order.buyer.firstName, + lastName: order.buyer.lastName, + email: order.buyer.lastName, + gender: order.buyer.gender, + phoneNumber: order.buyer.phoneNumber, + photoUrl: order.buyer.photoUrl, + }, + vendors: vendorOrders.map(vendoOrder => ({ + id: vendoOrder.vendor.id, + firstName: vendoOrder.vendor.firstName, + lastName: vendoOrder.vendor.lastName, + email: vendoOrder.vendor.lastName, + gender: vendoOrder.vendor.gender, + phoneNumber: vendoOrder.vendor.phoneNumber, + photoUrl: vendoOrder.vendor.photoUrl, + order: { + id: vendoOrder.id, + totalPrice: vendoOrder.totalPrice, + orderStatus: vendoOrder.orderStatus, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + orderItems: vendoOrder.vendorOrderItems, + }, + })), + }); + } + + responseSuccess(res, 200, 'Orders retrieved successfully', { + totalOrders: orders.length, + orders: sanitizedOrdersResponse, + }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; + +// Get single vendor order info +export const getSingleBuyerVendorOrderService = async (req: Request, res: Response) => { + try { + const orderId = req.params.id; + + const vendorOrderRepository = getRepository(VendorOrders); + const orderRepository = getRepository(Order); + + const order = await orderRepository.findOne({ + where: { + id: orderId, + }, + relations: ['buyer', 'orderItems'], + order: { + createdAt: 'DESC', // Order by creation date, most recent first + }, + }); + + if (!order) { + return responseError(res, 404, `Order Not Found.`); + } + + const vendorOrders = await vendorOrderRepository.find({ + where: { + order: { + id: order.id, + }, + }, + relations: ['vendor', 'vendorOrderItems', 'vendorOrderItems.product'], + }); + + const sanitizedOrderResponse = { + id: order.id, + totalPrice: order.totalPrice, + totalProducts: order.orderItems.length, + orderStatus: order.orderStatus, + address: order.address, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + buyer: { + id: order.buyer.id, + firstName: order.buyer.firstName, + lastName: order.buyer.lastName, + email: order.buyer.lastName, + gender: order.buyer.gender, + phoneNumber: order.buyer.phoneNumber, + photoUrl: order.buyer.photoUrl, + }, + vendors: vendorOrders.map(vendoOrder => ({ + id: vendoOrder.vendor.id, + firstName: vendoOrder.vendor.firstName, + lastName: vendoOrder.vendor.lastName, + email: vendoOrder.vendor.lastName, + gender: vendoOrder.vendor.gender, + phoneNumber: vendoOrder.vendor.phoneNumber, + photoUrl: vendoOrder.vendor.photoUrl, + order: { + id: vendoOrder.id, + totalPrice: vendoOrder.totalPrice, + orderStatus: vendoOrder.orderStatus, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + orderItems: vendoOrder.vendorOrderItems, + }, + })), + }; + + responseSuccess(res, 200, 'Order retrieved successfully', { + order: sanitizedOrderResponse, + }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; diff --git a/src/services/adminOrderServices/updateOrder.ts b/src/services/adminOrderServices/updateOrder.ts new file mode 100644 index 0000000..876160f --- /dev/null +++ b/src/services/adminOrderServices/updateOrder.ts @@ -0,0 +1,107 @@ +import { Request, Response } from 'express'; +import { Not, getRepository } from 'typeorm'; +import { responseSuccess, responseError } from '../../utils/response.utils'; +import { VendorOrderItem } from '../../entities/VendorOrderItem'; +import { VendorOrders } from '../../entities/vendorOrders'; +import { Order } from '../../entities/Order'; +import { getIO } from '../../utils/socket'; + +export const updateBuyerVendorOrderService = async (req: Request, res: Response) => { + try { + const orderId = req.params.id; + + const vendorOrderRepository = getRepository(VendorOrders); + const orderRepository = getRepository(Order); + + const order = await orderRepository.findOne({ + where: { + id: orderId, + }, + relations: ['buyer', 'orderItems'], + }); + + if (!order) { + return responseError(res, 404, `Order Not Found.`); + } + + if (order.orderStatus === 'completed') { + return responseError(res, 409, 'The order has already been completed.'); + } + + if (order.orderStatus !== 'received') { + return responseError(res, 409, 'Order closure failed: The buyer has not received the item yet.'); + } + + const vendorOrders = await vendorOrderRepository.find({ + where: { + order: { + id: order.id, + }, + }, + relations: ['vendor', 'vendorOrderItems', 'vendorOrderItems.product'], + }); + + for (const order of vendorOrders) { + if (order.orderStatus !== 'delivered') { + return responseError(res, 409, 'Order closure failed: Some vendors have not yet delivered items to the buyer.'); + } + } + + // Update Whole Order + + order.orderStatus = 'completed'; + await orderRepository.save(order); + + const updatedVendorOrder = vendorOrders.map(async order => { + order.orderStatus = 'completed'; + await vendorOrderRepository.save(order); + }); + + const sanitizedOrderResponse = { + id: order.id, + totalPrice: order.totalPrice, + totalProducts: order.orderItems.length, + orderStatus: order.orderStatus, + address: order.address, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + buyer: { + id: order.buyer.id, + firstName: order.buyer.firstName, + lastName: order.buyer.lastName, + email: order.buyer.lastName, + gender: order.buyer.gender, + phoneNumber: order.buyer.phoneNumber, + photoUrl: order.buyer.photoUrl, + }, + vendors: vendorOrders.map(vendoOrder => ({ + id: vendoOrder.vendor.id, + firstName: vendoOrder.vendor.firstName, + lastName: vendoOrder.vendor.lastName, + email: vendoOrder.vendor.lastName, + gender: vendoOrder.vendor.gender, + phoneNumber: vendoOrder.vendor.phoneNumber, + photoUrl: vendoOrder.vendor.photoUrl, + order: { + id: vendoOrder.id, + totalPrice: vendoOrder.totalPrice, + orderStatus: vendoOrder.orderStatus, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + orderItems: vendoOrder.vendorOrderItems, + }, + })), + }; + + getIO().emit('orders', { + action: 'admin update', + order: sanitizedOrderResponse, + }); + + responseSuccess(res, 200, 'Order updated successfully', { + order: sanitizedOrderResponse, + }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; diff --git a/src/services/couponServices/buyerApplyCoupon.ts b/src/services/couponServices/buyerApplyCoupon.ts index 12da4e1..85762f6 100644 --- a/src/services/couponServices/buyerApplyCoupon.ts +++ b/src/services/couponServices/buyerApplyCoupon.ts @@ -5,81 +5,83 @@ import { Cart } from '../../entities/Cart'; import { CartItem } from '../../entities/CartItem'; export const buyerApplyCouponService = async (req: Request, res: Response) => { - try { - const {couponCode} = req.body + try { + const { couponCode } = req.body; - if (!couponCode) return res.status(400).json({ message: 'Coupon Code is required' }); + if (!couponCode) return res.status(400).json({ message: 'Coupon Code is required' }); - const couponRepository = getRepository(Coupon); - const coupon = await couponRepository.findOne({ - where: { code: couponCode }, - relations: ['product'], - }); - - if(!coupon) return res.status(404).json({message: 'Invalid Coupon Code'}); + const couponRepository = getRepository(Coupon); + const coupon = await couponRepository.findOne({ + where: { code: couponCode }, + relations: ['product'], + }); - if(coupon){ - if(coupon.expirationDate && coupon.expirationDate < new Date()){ - return res.status(400).json({message: 'Coupon is expired'}); - } + if (!coupon) return res.status(404).json({ message: 'Invalid Coupon Code' }); - if(coupon.usageTimes == coupon.maxUsageLimit){ - return res.status(400).json({message: 'Coupon Discount Ended'}); - } + if (coupon) { + if (coupon.expirationDate && coupon.expirationDate < new Date()) { + return res.status(400).json({ message: 'Coupon is expired' }); } - const couponProductId = coupon.product.id; - - const cartRepository = getRepository(Cart) - let cart = await cartRepository.findOne({where: { user: { id: req.user?.id },isCheckedOut: false }, - relations: ['items', 'items.product'], - }); - - if(!cart) return res.status(400).json({message: "You don't have a product in cart"}); - const cartItemRepository = getRepository(CartItem); - const couponCartItem = await cartItemRepository.findOne({ - where: { - cart: { id: cart.id }, - product: { id: couponProductId }, - }, - relations: ['product'], - }); - - if(!couponCartItem) return res.status(404).json({message: 'No product in Cart with that coupon code'}); - - let amountReducted; - if(coupon.discountType === 'percentage'){ - const reduction = (couponCartItem.product.newPrice * coupon.discountRate)/ 100; - amountReducted = reduction; - couponCartItem.newPrice = couponCartItem.product.newPrice - reduction; - - await cartItemRepository.save(couponCartItem) - } - else { - amountReducted = coupon.discountRate; - couponCartItem.newPrice = couponCartItem.product.newPrice - amountReducted; - await cartItemRepository.save(couponCartItem) - } - - cart = await cartRepository.findOne({where: { id: cart.id}, - relations: ['items', 'items.product'], - }); - if(cart){ - cart.updateTotal(); - await cartRepository.save(cart); + if (coupon.usageTimes == coupon.maxUsageLimit) { + return res.status(400).json({ message: 'Coupon Discount Ended' }); } + } + const couponProductId = coupon.product.id; + + const cartRepository = getRepository(Cart); + let cart = await cartRepository.findOne({ + where: { user: { id: req.user?.id }, isCheckedOut: false }, + relations: ['items', 'items.product'], + }); + + if (!cart) return res.status(400).json({ message: "You don't have a product in cart" }); + + const cartItemRepository = getRepository(CartItem); + const couponCartItem = await cartItemRepository.findOne({ + where: { + cart: { id: cart.id }, + product: { id: couponProductId }, + }, + relations: ['product'], + }); + + if (!couponCartItem) return res.status(404).json({ message: 'No product in Cart with that coupon code' }); + + let amountReducted; + if (coupon.discountType === 'percentage') { + const reduction = (couponCartItem.product.newPrice * coupon.discountRate) / 100; + amountReducted = reduction; + couponCartItem.newPrice = couponCartItem.product.newPrice - reduction; + + await cartItemRepository.save(couponCartItem); + } else { + amountReducted = coupon.discountRate; + couponCartItem.newPrice = couponCartItem.product.newPrice - amountReducted; + await cartItemRepository.save(couponCartItem); + } - coupon.usageTimes +=1; + cart = await cartRepository.findOne({ where: { id: cart.id }, relations: ['items', 'items.product'] }); + if (cart) { + cart.updateTotal(); + await cartRepository.save(cart); + } - if(req.user?.id){ - coupon.usedBy.push(req.user?.id); - } + coupon.usageTimes += 1; - await couponRepository.save(coupon); + if (req.user?.id) { + coupon.usedBy.push(req.user?.id); + } - return (res.status(200).json({message: `Coupon Code successfully activated discount on product: ${couponCartItem.product.name}`, amountDiscounted: amountReducted })); + await couponRepository.save(coupon); - } catch (error) { - return res.status(500).json({ error: 'Internal server error' }); - } - }; \ No newline at end of file + return res + .status(200) + .json({ + message: `Coupon Code successfully activated discount on product: ${couponCartItem.product.name}`, + amountDiscounted: amountReducted, + }); + } catch (error) { + return res.status(500).json({ error: 'Internal server error' }); + } +}; diff --git a/src/services/index.ts b/src/services/index.ts index 8f560c3..12d0aa7 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -33,3 +33,11 @@ export * from './cartServices/createCart'; export * from './cartServices/readCart'; export * from './cartServices/removeProductInCart'; export * from './cartServices/clearCart'; + +// vendor order management +export * from './vendorOrderServices/readVendorOrder'; +export * from './vendorOrderServices/updateVendorOrder'; + +// vendor order management +export * from './adminOrderServices/readOrder'; +export * from './adminOrderServices/updateOrder'; diff --git a/src/services/orderServices/createOrder.ts b/src/services/orderServices/createOrder.ts index 038d796..30cbb4a 100644 --- a/src/services/orderServices/createOrder.ts +++ b/src/services/orderServices/createOrder.ts @@ -8,6 +8,9 @@ import { Cart } from '../../entities/Cart'; import { Transaction } from '../../entities/transaction'; import { responseError, sendErrorResponse, sendSuccessResponse } from '../../utils/response.utils'; import sendMail from '../../utils/sendOrderMail'; +import { VendorOrders } from '../../entities/vendorOrders'; +import { CartItem } from '../../entities/CartItem'; +import { VendorOrderItem } from '../../entities/VendorOrderItem'; export const createOrderService = async (req: Request, res: Response) => { const { cartId, address } = req.body; @@ -76,7 +79,6 @@ export const createOrderService = async (req: Request, res: Response) => { await getManager().transaction(async transactionalEntityManager => { for (const item of cart.items) { const product = item.product; - product.quantity -= item.quantity; await transactionalEntityManager.save(Product, product); } @@ -118,14 +120,67 @@ export const createOrderService = async (req: Request, res: Response) => { const message = { subject: 'Order created successfully', - ...orderResponse + ...orderResponse, }; await sendMail(message); + // separate order by each vendor getting order related to his products + await saveVendorRelatedOrder(newOrder, cart.items); + return sendSuccessResponse(res, 201, 'Order created successfully', orderResponse); } catch (error) { - console.error('Error creating order:', error); return sendErrorResponse(res, 500, (error as Error).message); } -}; \ No newline at end of file +}; + +const saveVendorRelatedOrder = async (order: Order, CartItem: CartItem[]) => { + try { + for (const item of CartItem) { + const productRepository = getRepository(Product); + + const product = await productRepository.findOne({ + where: { + id: item.product.id, + }, + relations: ['vendor'], + }); + + if (!product) return; + + const orderItem = new VendorOrderItem(); + orderItem.product = product; + orderItem['price/unit'] = product.newPrice; + orderItem.quantity = item.quantity; + + const vendorOrdersRepository = getRepository(VendorOrders); + let vendorOrders = await vendorOrdersRepository.findOne({ + where: { + vendor: { + id: product.vendor.id, + }, + order: { + id: order.id, + }, + }, + relations: ['vendorOrderItems'], + }); + + if (vendorOrders) { + vendorOrders.totalPrice = Number(vendorOrders.totalPrice) + +product.newPrice * +item.quantity; + vendorOrders.vendorOrderItems = [...vendorOrders.vendorOrderItems, orderItem]; + } else { + const newVendorOrders = new VendorOrders(); + newVendorOrders.vendor = product.vendor; + newVendorOrders.vendorOrderItems = [orderItem]; + newVendorOrders.order = order; + newVendorOrders.totalPrice = +product.newPrice * item.quantity; + vendorOrders = newVendorOrders; + } + + await vendorOrdersRepository.save(vendorOrders); + } + } catch (error) { + console.log((error as Error).message); + } +}; diff --git a/src/services/orderServices/getOrderService.ts b/src/services/orderServices/getOrderService.ts index 18e0664..4208123 100644 --- a/src/services/orderServices/getOrderService.ts +++ b/src/services/orderServices/getOrderService.ts @@ -4,62 +4,60 @@ import { responseSuccess, responseError } from '../../utils/response.utils'; import { Order } from '../../entities/Order'; import { OrderItem } from '../../entities/OrderItem'; - // Example usage: - export const getOrdersService = async (req: Request, res: Response) => { - try { - const orderRepository = getRepository(Order); - const buyerId = req.user?.id; + try { + const orderRepository = getRepository(Order); + const buyerId = req.user?.id; - const orders = await orderRepository.find({ - where: { - buyer: { - id: buyerId, - } - }, - relations: ['buyer', 'orderItems', 'orderItems.product'], - order: { - createdAt: 'DESC', // Order by creation date, most recent first - }, - }); + const orders = await orderRepository.find({ + where: { + buyer: { + id: buyerId, + }, + }, + relations: ['buyer', 'orderItems', 'orderItems.product'], + order: { + createdAt: 'DESC', // Order by creation date, most recent first + }, + }); - if (!orders || orders.length === 0) { - return responseSuccess(res, 404, `You haven't made any orders yet`, { orders: [] }); - } - - const sanitezedResponse = orders.map(order => ({ - id: order.id, - totalPrice: order.totalPrice, - orderStatus: order.orderStatus, - quantity: order.quantity, - address: order.address, - orderDate: order.orderDate, - createdAt: order.createdAt, - updatedAt: order.updatedAt, - buyer: { - id: order.buyer.id, - firstName: order.buyer.firstName, - lastName: order.buyer.lastName, - accountBalance: order.buyer.accountBalance - }, - orderItems: order.orderItems.map((item: OrderItem) => ({ - id: item.id, - price: item.price, - quantity: item.quantity, - product: { - id: item.product.id, - name: item.product.name, - description: item.product.description, - images: item.product.images, - price: item.product.newPrice, - expirationDate: item.product.expirationDate, - } - })) - })); - responseSuccess(res, 200, 'Orders retrieved successfully', { orders: sanitezedResponse }); - } catch (error) { - return responseError(res, 400, (error as Error).message); + if (!orders || orders.length === 0) { + return responseSuccess(res, 404, `You haven't made any orders yet`, { orders: [] }); } -}; \ No newline at end of file + + const sanitezedResponse = orders.map(order => ({ + id: order.id, + totalPrice: order.totalPrice, + orderStatus: order.orderStatus, + quantity: order.quantity, + address: order.address, + orderDate: order.orderDate, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + buyer: { + id: order.buyer.id, + firstName: order.buyer.firstName, + lastName: order.buyer.lastName, + accountBalance: order.buyer.accountBalance, + }, + orderItems: order.orderItems.map((item: OrderItem) => ({ + id: item.id, + price: item.price, + quantity: item.quantity, + product: { + id: item.product.id, + name: item.product.name, + description: item.product.description, + images: item.product.images, + price: item.product.newPrice, + expirationDate: item.product.expirationDate, + }, + })), + })); + responseSuccess(res, 200, 'Orders retrieved successfully', { orders: sanitezedResponse }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; diff --git a/src/services/orderServices/updateOrderService.ts b/src/services/orderServices/updateOrderService.ts index f29b47c..82a043a 100644 --- a/src/services/orderServices/updateOrderService.ts +++ b/src/services/orderServices/updateOrderService.ts @@ -8,122 +8,129 @@ import { Transaction } from '../../entities/transaction'; import { responseError, sendErrorResponse, sendSuccessResponse } from '../../utils/response.utils'; import sendMail from '../../utils/sendOrderMail'; interface OrderStatusType { - orderStatus: 'order placed' | 'cancelled' | 'awaiting shipment' | 'in transit' | 'delivered' | 'received' | 'returned'; + orderStatus: + | 'order placed' + | 'cancelled' + | 'awaiting shipment' + | 'in transit' + | 'delivered' + | 'received' + | 'returned'; } export const updateOrderService = async (req: Request, res: Response) => { - const { orderId } = req.params; - const { orderStatus } = req.body; - - try { - await getManager().transaction(async (transactionalEntityManager: EntityManager) => { - const orderRepository: Repository = transactionalEntityManager.getRepository(Order); - const productRepository: Repository = transactionalEntityManager.getRepository(Product); - const userRepository: Repository = transactionalEntityManager.getRepository(User); - const orderItemRepository: Repository = transactionalEntityManager.getRepository(OrderItem); - const transactionRepository: Repository = transactionalEntityManager.getRepository(Transaction); - - const buyerId = req.user?.id; - if (!buyerId) { - throw new Error('Unauthorized'); + const { orderId } = req.params; + const { orderStatus } = req.body; + + try { + await getManager().transaction(async (transactionalEntityManager: EntityManager) => { + const orderRepository: Repository = transactionalEntityManager.getRepository(Order); + const productRepository: Repository = transactionalEntityManager.getRepository(Product); + const userRepository: Repository = transactionalEntityManager.getRepository(User); + const orderItemRepository: Repository = transactionalEntityManager.getRepository(OrderItem); + const transactionRepository: Repository = transactionalEntityManager.getRepository(Transaction); + + const buyerId = req.user?.id; + if (!buyerId) { + throw new Error('Unauthorized'); + } + + // Fetch order and related entities + const order: Order | null = await orderRepository.findOne({ + where: { id: orderId, buyer: { id: buyerId } }, + relations: ['orderItems', 'orderItems.product', 'buyer'], + }); + + if (!order) { + return sendErrorResponse(res, 404, 'Order not found'); + } + // Check if order can be updated + if (isOrderFinalStatus(order.orderStatus)) { + return sendErrorResponse(res, 401, `Order cannot be updated once it is ${order.orderStatus}`); + } + + // Handle order status transitions + if (orderStatus !== undefined && order.orderStatus !== orderStatus) { + switch (orderStatus) { + case 'cancelled': + case 'returned': + if (order.orderStatus !== 'delivered') { + await processRefund(order, transactionalEntityManager); } - - // Fetch order and related entities - const order: Order | null = await orderRepository.findOne({ - where: { id: orderId, buyer: { id: buyerId } }, - relations: ['orderItems', 'orderItems.product', 'buyer'], - }); - - if (!order) { - return sendErrorResponse(res, 404, "Order not found"); - } - // Check if order can be updated - if (isOrderFinalStatus(order.orderStatus)) { - return sendErrorResponse(res, 401, `Order cannot be updated once it is ${order.orderStatus}`); - } - - // Handle order status transitions - if (orderStatus !== undefined && order.orderStatus !== orderStatus) { - switch (orderStatus) { - case 'cancelled': - case 'returned': - if (order.orderStatus !== 'delivered') { - await processRefund(order, transactionalEntityManager); - } - break; - default: - break; - } - - order.orderStatus = orderStatus; - } - - // Save updated order status - await orderRepository.save(order); - - // Prepare response data - const orderResponse = { - fullName: `${order.buyer.firstName} ${order.buyer.lastName}`, - email: order.buyer.email, - products: order.orderItems.map((item: OrderItem) => ({ - name: item.product.name, - newPrice: item.price, - quantity: item.quantity, - })), - totalAmount: order.totalPrice, - quantity: order.quantity, - orderDate: order.orderDate, - address: order.address, - }; - - // Send email notification - const message = { - subject: 'Order updated successfully', - ...orderResponse - }; - await sendMail(message); - - // Respond with success - return sendSuccessResponse(res, 200, 'Order updated successfully', orderResponse); - }); - } catch (error) { - console.error('Error updating order:', error); - return sendErrorResponse(res, 500, (error as Error).message); - } + break; + default: + break; + } + + order.orderStatus = orderStatus; + } + + // Save updated order status + await orderRepository.save(order); + + // Prepare response data + const orderResponse = { + fullName: `${order.buyer.firstName} ${order.buyer.lastName}`, + email: order.buyer.email, + products: order.orderItems.map((item: OrderItem) => ({ + name: item.product.name, + newPrice: item.price, + quantity: item.quantity, + })), + totalAmount: order.totalPrice, + quantity: order.quantity, + orderDate: order.orderDate, + address: order.address, + }; + + // Send email notification + const message = { + subject: 'Order updated successfully', + ...orderResponse, + }; + await sendMail(message); + + // Respond with success + return sendSuccessResponse(res, 200, 'Order updated successfully', orderResponse); + }); + } catch (error) { + console.error('Error updating order:', error); + return sendErrorResponse(res, 500, (error as Error).message); + } }; -async function processRefund(order: Order, entityManager: EntityManager) { - const buyer = order.buyer; - - // Refund buyer - const previousBalance = buyer.accountBalance; - buyer.accountBalance += order.totalPrice; - const currentBalance = buyer.accountBalance; - await entityManager.save(buyer); - - // Record refund transaction - const refundTransaction = new Transaction(); - refundTransaction.user = buyer; - refundTransaction.order = order; - refundTransaction.amount = order.totalPrice; - refundTransaction.previousBalance = previousBalance; - refundTransaction.currentBalance = currentBalance; - refundTransaction.type = 'credit'; - refundTransaction.description = 'Refund for cancelled or returned order'; - await entityManager.save(refundTransaction); - - // Return products to store - for (const orderItem of order.orderItems) { - const product = orderItem.product; - product.quantity += orderItem.quantity; - await entityManager.save(product); - } - - // Clear order details - order.orderItems = []; - order.totalPrice = 0; - order.quantity = 0; +async function processRefund (order: Order, entityManager: EntityManager) { + const buyer = order.buyer; + + // Refund buyer + const previousBalance = buyer.accountBalance; + buyer.accountBalance += order.totalPrice; + const currentBalance = buyer.accountBalance; + await entityManager.save(buyer); + + // Record refund transaction + const refundTransaction = new Transaction(); + refundTransaction.user = buyer; + refundTransaction.order = order; + refundTransaction.amount = order.totalPrice; + refundTransaction.previousBalance = previousBalance; + refundTransaction.currentBalance = currentBalance; + refundTransaction.type = 'credit'; + refundTransaction.description = 'Refund for cancelled or returned order'; + await entityManager.save(refundTransaction); + + // Return products to store + for (const orderItem of order.orderItems) { + const product = orderItem.product; + product.quantity += orderItem.quantity; + await entityManager.save(product); + } + + // Clear order details + order.orderItems = []; + order.totalPrice = 0; + order.quantity = 0; } -function isOrderFinalStatus(status: string): boolean { - return ['cancelled', 'delivered', 'returned'].includes(status); -} \ No newline at end of file +function isOrderFinalStatus (status: string): boolean { + return ['cancelled', 'delivered', 'returned', 'completed'].includes(status); +} diff --git a/src/services/productServices/deleteProduct.ts b/src/services/productServices/deleteProduct.ts index 43ec3d1..068c4c9 100644 --- a/src/services/productServices/deleteProduct.ts +++ b/src/services/productServices/deleteProduct.ts @@ -3,30 +3,28 @@ import { Product } from '../../entities/Product'; import { getRepository } from 'typeorm'; import { responseError, responseSuccess } from '../../utils/response.utils'; - export const deleteProductService = async (req: Request, res: Response) => { - try { - const { id } = req.params; - - const productRepository = getRepository(Product); + try { + const { id } = req.params; - const product = await productRepository.findOne({ - where: { - id: id, - vendor: { - id: req.user?.id - } - } - }); + const productRepository = getRepository(Product); - if (product) { - await productRepository.remove(product); - return responseSuccess(res, 200, 'Product successfully deleted'); - } + const product = await productRepository.findOne({ + where: { + id: id, + vendor: { + id: req.user?.id, + }, + }, + }); - return responseError(res, 404, 'Product not found'); - - } catch (error) { - responseError(res, 400, (error as Error).message); + if (product) { + await productRepository.remove(product); + return responseSuccess(res, 200, 'Product successfully deleted'); } + + return responseError(res, 404, 'Product not found'); + } catch (error) { + responseError(res, 400, (error as Error).message); + } }; diff --git a/src/services/productServices/getRecommendedProductsService.ts b/src/services/productServices/getRecommendedProductsService.ts index 19368e1..fde015d 100644 --- a/src/services/productServices/getRecommendedProductsService.ts +++ b/src/services/productServices/getRecommendedProductsService.ts @@ -1,62 +1,63 @@ -import { Request, Response } from "express"; -import { responseError, responseSuccess } from "../../utils/response.utils"; -import { getRepository } from "typeorm"; -import { Product } from "../../entities/Product"; +import { Request, Response } from 'express'; +import { responseError, responseSuccess } from '../../utils/response.utils'; +import { getRepository } from 'typeorm'; +import { Product } from '../../entities/Product'; interface conditionDoc { - categories: any[] | null; - vendor: any | null + categories: any[] | null; + vendor: any | null; } export const getRecommendedProductsService = async (req: Request, res: Response) => { + try { + // Define pagination parameters + const page = req.query.page ? Number(req.query.page) : 1; + const limit = req.query.limit ? Number(req.query.limit) : 10; + const skip = (page - 1) * limit; + const condition: conditionDoc = { + categories: null, + vendor: null, + }; - try { - // Define pagination parameters - const page = req.query.page ? Number(req.query.page) : 1; - const limit = req.query.limit ? Number(req.query.limit) : 10; - const skip = (page - 1) * limit; - const condition: conditionDoc = { - categories: null, - vendor: null - }; - - if (req.query.categories) { - const categoryIds = Array.isArray(req.query.categories) ? req.query.categories : [req.query.categories]; - condition.categories = categoryIds; - }; - if (req.query.vendor) condition.vendor = req.query.vendor; + if (req.query.categories) { + const categoryIds = Array.isArray(req.query.categories) ? req.query.categories : [req.query.categories]; + condition.categories = categoryIds; + } + if (req.query.vendor) condition.vendor = req.query.vendor; - const productRepository = getRepository(Product); - const productsQuery = productRepository.createQueryBuilder("product") - .leftJoinAndSelect("product.categories", "category") - .leftJoinAndSelect("product.vendor", "vendor") - .where("1 = 1"); + const productRepository = getRepository(Product); + const productsQuery = productRepository + .createQueryBuilder('product') + .leftJoinAndSelect('product.categories', 'category') + .leftJoinAndSelect('product.vendor', 'vendor') + .where('1 = 1'); - if (condition.categories && condition.categories.length > 0) { - productsQuery.andWhere("category.id IN (:...categories)", { categories: condition.categories }); - } - if (condition.vendor) { - productsQuery.andWhere("vendor.id = :vendorId", { vendorId: condition.vendor }); - } + if (condition.categories && condition.categories.length > 0) { + productsQuery.andWhere('category.id IN (:...categories)', { categories: condition.categories }); + } + if (condition.vendor) { + productsQuery.andWhere('vendor.id = :vendorId', { vendorId: condition.vendor }); + } - const products = await productsQuery - .skip(skip) - .take(limit) - .getMany(); - if (products.length < 1) { - return responseSuccess(res, 200, `No products found for the specified ${condition.vendor ? 'vendor' : 'category'}`); - } - const sanitizedProducts = products.map(product => ({ - ...product, - vendor: { - firstName: product.vendor.firstName, - lastName: product.vendor.lastName, - phoneNumber: product.vendor.phoneNumber, - photoUrl: product.vendor.photoUrl - } - })); - return responseSuccess(res, 200, 'Products retrieved', { products: sanitizedProducts }); - } catch (error) { - return responseError(res, 400, (error as Error).message); + const products = await productsQuery.skip(skip).take(limit).getMany(); + if (products.length < 1) { + return responseSuccess( + res, + 200, + `No products found for the specified ${condition.vendor ? 'vendor' : 'category'}` + ); } -}; \ No newline at end of file + const sanitizedProducts = products.map(product => ({ + ...product, + vendor: { + firstName: product.vendor.firstName, + lastName: product.vendor.lastName, + phoneNumber: product.vendor.phoneNumber, + photoUrl: product.vendor.photoUrl, + }, + })); + return responseSuccess(res, 200, 'Products retrieved', { products: sanitizedProducts }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; diff --git a/src/services/productServices/listAllProductsService.ts b/src/services/productServices/listAllProductsService.ts index 8950abd..f39c7bb 100644 --- a/src/services/productServices/listAllProductsService.ts +++ b/src/services/productServices/listAllProductsService.ts @@ -5,38 +5,40 @@ import { responseError, responseSuccess } from '../../utils/response.utils'; import { validate } from 'uuid'; export const listAllProductsService = async (req: Request, res: Response) => { - try { - const page = req.query.page ? Number(req.query.page) : 1; - const limit = req.query.limit ? Number(req.query.limit) : 10; - const skip = (page - 1) * limit; - const category = req.query.category ; + try { + const page = req.query.page ? Number(req.query.page) : 1; + const limit = req.query.limit ? Number(req.query.limit) : 10; + const skip = (page - 1) * limit; + const category = req.query.category; - - const productRepository = getRepository(Product); - const products = await productRepository.find({ - where: { - categories: { - name: category as string - } - }, - skip, - take: limit, - relations: ["categories","vendor"], - select: { - vendor: { - id: true, firstName: true, lastName: true, - email: true, phoneNumber: true, photoUrl: true - } - } - } - ); + const productRepository = getRepository(Product); + const products = await productRepository.find({ + where: { + categories: { + name: category as string, + }, + }, + skip, + take: limit, + relations: ['categories', 'vendor'], + select: { + vendor: { + id: true, + firstName: true, + lastName: true, + email: true, + phoneNumber: true, + photoUrl: true, + }, + }, + }); - if (products.length < 1) { - return responseSuccess(res, 200, 'No products found'); - } - - return responseSuccess(res, 200, 'Products retrieved', { products }); - } catch (error) { - responseError(res, 400, (error as Error).message); + if (products.length < 1) { + return responseSuccess(res, 200, 'No products found'); } + + return responseSuccess(res, 200, 'Products retrieved', { products }); + } catch (error) { + responseError(res, 400, (error as Error).message); + } }; diff --git a/src/services/productServices/readProduct.ts b/src/services/productServices/readProduct.ts index 5c9257c..b3c244d 100644 --- a/src/services/productServices/readProduct.ts +++ b/src/services/productServices/readProduct.ts @@ -4,67 +4,75 @@ import { getRepository } from 'typeorm'; import { responseError, responseSuccess } from '../../utils/response.utils'; export const readProductsService = async (req: Request, res: Response) => { - try { - // Define pagination parameters - const page = req.query.page ? Number(req.query.page) : 1; - const limit = req.query.limit ? Number(req.query.limit) : 10; - const skip = (page - 1) * limit; + try { + // Define pagination parameters + const page = req.query.page ? Number(req.query.page) : 1; + const limit = req.query.limit ? Number(req.query.limit) : 10; + const skip = (page - 1) * limit; - // Retrieve products - const productRepository = getRepository(Product); - const products = await productRepository.find({ - where: { - vendor: { - id: req.user?.id, - }, - }, - skip, - take: limit, - relations: ['categories', 'vendor'], - select: { - vendor: { - id: true, firstName: true, lastName: true, - email: true, phoneNumber: true, photoUrl: true - } - } - }); + // Retrieve products + const productRepository = getRepository(Product); + const products = await productRepository.find({ + where: { + vendor: { + id: req.user?.id, + }, + }, + skip, + take: limit, + relations: ['categories', 'vendor'], + select: { + vendor: { + id: true, + firstName: true, + lastName: true, + email: true, + phoneNumber: true, + photoUrl: true, + }, + }, + }); - if (products.length < 1) { - return responseSuccess(res, 200, 'You have no products yet'); - } - return responseSuccess(res, 200, 'Products retrieved', { products }); - } catch (error) { - responseError(res, 400, (error as Error).message); + if (products.length < 1) { + return responseSuccess(res, 200, 'You have no products yet'); } + return responseSuccess(res, 200, 'Products retrieved', { products }); + } catch (error) { + responseError(res, 400, (error as Error).message); + } }; export const readProductService = async (req: Request, res: Response) => { - try { - const { id } = req.params; + try { + const { id } = req.params; - const productRepository = getRepository(Product); - const product = await productRepository.findOne({ - where: { - id: id, - vendor: { - id: req.user?.id, - }, - }, - relations: ['categories', 'vendor'], - select: { - vendor: { - id: true, firstName: true, lastName: true, - email: true, phoneNumber: true, photoUrl: true - } - } - }); + const productRepository = getRepository(Product); + const product = await productRepository.findOne({ + where: { + id: id, + vendor: { + id: req.user?.id, + }, + }, + relations: ['categories', 'vendor'], + select: { + vendor: { + id: true, + firstName: true, + lastName: true, + email: true, + phoneNumber: true, + photoUrl: true, + }, + }, + }); - if (!product) { - return responseError(res, 404, 'Product not found'); - } - - return responseSuccess(res, 200, 'Product retrieved', { product }); - } catch (error) { - responseError(res, 400, (error as Error).message); + if (!product) { + return responseError(res, 404, 'Product not found'); } + + return responseSuccess(res, 200, 'Product retrieved', { product }); + } catch (error) { + responseError(res, 400, (error as Error).message); + } }; diff --git a/src/services/productServices/removeProductImage.ts b/src/services/productServices/removeProductImage.ts index 2995593..4424676 100644 --- a/src/services/productServices/removeProductImage.ts +++ b/src/services/productServices/removeProductImage.ts @@ -21,7 +21,7 @@ export const removeProductImageService = async (req: Request, res: Response) => const product = await productRepository.findOne({ where: { id, - vendor: { id: req.user?.id } + vendor: { id: req.user?.id }, }, relations: ['vendor'], }); diff --git a/src/services/productServices/searchProduct.ts b/src/services/productServices/searchProduct.ts index 765f431..9f33b5f 100644 --- a/src/services/productServices/searchProduct.ts +++ b/src/services/productServices/searchProduct.ts @@ -1,4 +1,4 @@ -import { Request, Response } from "express"; +import { Request, Response } from 'express'; import { getRepository, Like } from 'typeorm'; import { Product } from '../../entities/Product'; @@ -26,10 +26,7 @@ export const searchProductService = async (params: SearchProductParams) => { const skip = (page - 1) * limit; - const [products, total] = await query - .skip(skip) - .take(limit) - .getManyAndCount(); + const [products, total] = await query.skip(skip).take(limit).getManyAndCount(); const totalPages = Math.ceil(total / limit); diff --git a/src/services/productServices/viewSingleProduct.ts b/src/services/productServices/viewSingleProduct.ts index f956625..29ac167 100644 --- a/src/services/productServices/viewSingleProduct.ts +++ b/src/services/productServices/viewSingleProduct.ts @@ -4,35 +4,28 @@ import { getRepository } from 'typeorm'; import { responseError } from '../../utils/response.utils'; import { validate } from 'uuid'; - - export const viewSingleProduct = async (req: Request, res: Response) => { - try { - const productId = req.params.id; + try { + const productId = req.params.id; + + if (!validate(productId)) { + return res.status(400).json({ status: 'error', message: 'Invalid product ID' }); + } + if (productId) { + const products = getRepository(Product); + const product = await products.findOneBy({ id: productId }); - if (!validate(productId)) { - return res.status(400).json({ status: 'error', message: 'Invalid product ID' }); - + if (!product) { + return res.status(404).send({ status: 'error', message: 'Product not found' }); } - if(productId){ - const products = getRepository(Product); - const product = await products.findOneBy({ id: productId }); - - if (!product) { - return res.status(404).send({status:'error', message: 'Product not found'}); - - } - - if (product.expirationDate && new Date(product.expirationDate) < new Date()) { - return res.status(400).json({ status: 'error', message: 'Product expired' }); - - } - res.status(200).json({ status: 'success', product: product }); + if (product.expirationDate && new Date(product.expirationDate) < new Date()) { + return res.status(400).json({ status: 'error', message: 'Product expired' }); } - - } catch (error) { - console.error('Error handling request:', error); - res.status(500).send('Error fetching product details'); + res.status(200).json({ status: 'success', product: product }); } -} \ No newline at end of file + } catch (error) { + console.error('Error handling request:', error); + res.status(500).send('Error fetching product details'); + } +}; diff --git a/src/services/vendorOrderServices/readVendorOrder.ts b/src/services/vendorOrderServices/readVendorOrder.ts new file mode 100644 index 0000000..feec0c3 --- /dev/null +++ b/src/services/vendorOrderServices/readVendorOrder.ts @@ -0,0 +1,119 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { responseSuccess, responseError } from '../../utils/response.utils'; +import { VendorOrderItem } from '../../entities/VendorOrderItem'; +import { VendorOrders } from '../../entities/vendorOrders'; + +export const getVendorOrdersService = async (req: Request, res: Response) => { + try { + const vendorOrderRepository = getRepository(VendorOrders); + const vendorId = req.user?.id; + + const vendorOrders = await vendorOrderRepository.find({ + where: { + vendor: { + id: vendorId, + }, + }, + relations: ['vendor', 'order.buyer', 'vendorOrderItems', 'vendorOrderItems.product'], + order: { + createdAt: 'DESC', // Order by creation date, most recent first + }, + }); + + if (!vendorOrders.length) { + return responseError(res, 200, `You don't have any pending orders from buyer`, { orders: [] }); + } + + const sanitizedOrderResponse = vendorOrders.map(order => ({ + id: order.id, + totalPrice: order.totalPrice, + orderStatus: order.orderStatus, + address: order.order.address, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + vendor: { + id: order.vendor.id, + firstName: order.vendor.firstName, + lastName: order.vendor.lastName, + email: order.vendor.email, + gender: order.vendor.gender, + phoneNumber: order.vendor.phoneNumber, + photoUrl: order.vendor.photoUrl, + }, + buyer: { + id: order.order.buyer.id, + firstName: order.order.buyer.firstName, + lastName: order.order.buyer.lastName, + email: order.order.buyer.lastName, + gender: order.order.buyer.gender, + phoneNumber: order.order.buyer.phoneNumber, + photoUrl: order.order.buyer.photoUrl, + }, + vendorOrderItems: order.vendorOrderItems, + })); + responseSuccess(res, 200, 'Orders retrieved successfully', { + orders: sanitizedOrderResponse, + }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; + +// Get single vendor order info +export const getSingleVendorOrderService = async (req: Request, res: Response) => { + try { + const vendorOrderId = req.params.id; + + const vendorOrderRepository = getRepository(VendorOrders); + const vendorId = req.user?.id; + + const vendorOrder = await vendorOrderRepository.findOne({ + where: { + id: vendorOrderId, + vendor: { + id: vendorId, + }, + }, + relations: ['vendor', 'order.buyer', 'vendorOrderItems', 'vendorOrderItems.product'], + }); + + if (!vendorOrder) { + return responseError(res, 404, `Order Not Found.`); + } + + const sanitizedOrderResponse = { + id: vendorOrder.id, + totalPrice: vendorOrder.totalPrice, + orderStatus: vendorOrder.orderStatus, + address: vendorOrder.order.address, + createdAt: vendorOrder.createdAt, + updatedAt: vendorOrder.updatedAt, + vendor: { + id: vendorOrder.vendor.id, + firstName: vendorOrder.vendor.firstName, + lastName: vendorOrder.vendor.lastName, + email: vendorOrder.vendor.email, + gender: vendorOrder.vendor.gender, + phoneNumber: vendorOrder.vendor.phoneNumber, + photoUrl: vendorOrder.vendor.photoUrl, + }, + buyer: { + id: vendorOrder.order.buyer.id, + firstName: vendorOrder.order.buyer.firstName, + lastName: vendorOrder.order.buyer.lastName, + email: vendorOrder.order.buyer.lastName, + gender: vendorOrder.order.buyer.gender, + phoneNumber: vendorOrder.order.buyer.phoneNumber, + photoUrl: vendorOrder.order.buyer.photoUrl, + }, + vendorOrderItems: vendorOrder.vendorOrderItems, + }; + + responseSuccess(res, 200, 'Order retrieved successfully', { + order: sanitizedOrderResponse, + }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; diff --git a/src/services/vendorOrderServices/updateVendorOrder.ts b/src/services/vendorOrderServices/updateVendorOrder.ts new file mode 100644 index 0000000..cae2c60 --- /dev/null +++ b/src/services/vendorOrderServices/updateVendorOrder.ts @@ -0,0 +1,87 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { OrderItem } from '../../entities/OrderItem'; +import { responseError, responseSuccess } from '../../utils/response.utils'; +import sendMail from '../../utils/sendOrderMail'; +import { VendorOrders } from '../../entities/vendorOrders'; +import { getIO } from '../../utils/socket'; + +export const updateVendorOrderService = async (req: Request, res: Response) => { + try { + const vendorOrderId = req.params.id; + const { orderStatus } = req.body; + if ( + !['pending', 'is-accepted', 'in-transit', 'cancelled', 'delivered'].includes( + (orderStatus as string).toLowerCase() + ) + ) { + return responseError(res, 400, `Please provide one of defined statuses.`); + } + + const vendorOrderRepository = getRepository(VendorOrders); + const vendorId = req.user?.id; + + const vendorOrder = await vendorOrderRepository.findOne({ + where: { + id: vendorOrderId, + vendor: { + id: vendorId, + }, + }, + relations: ['vendor', 'order.buyer', 'vendorOrderItems', 'vendorOrderItems.product'], + }); + + if (!vendorOrder) { + return responseError(res, 404, `Order Not Found.`); + } + + // Check if order can be updated + if (['delivered', 'cancelled', 'completed'].includes(vendorOrder.orderStatus)) { + return responseError(res, 409, `Order cannot be updated once it is marked as ${vendorOrder.orderStatus}`); + } + + vendorOrder.orderStatus = (orderStatus as string).toLowerCase(); + + // Save updated order status + const updatedVendorOrder = await vendorOrderRepository.save(vendorOrder); + + const sanitizedOrderResponse = { + id: updatedVendorOrder.id, + totalPrice: updatedVendorOrder.totalPrice, + orderStatus: updatedVendorOrder.orderStatus, + address: updatedVendorOrder.order.address, + createdAt: updatedVendorOrder.createdAt, + updatedAt: updatedVendorOrder.updatedAt, + vendor: { + id: updatedVendorOrder.vendor.id, + firstName: updatedVendorOrder.vendor.firstName, + lastName: updatedVendorOrder.vendor.lastName, + email: updatedVendorOrder.vendor.email, + gender: updatedVendorOrder.vendor.gender, + phoneNumber: updatedVendorOrder.vendor.phoneNumber, + photoUrl: updatedVendorOrder.vendor.photoUrl, + }, + buyer: { + id: updatedVendorOrder.order.buyer.id, + firstName: updatedVendorOrder.order.buyer.firstName, + lastName: updatedVendorOrder.order.buyer.lastName, + email: updatedVendorOrder.order.buyer.lastName, + gender: updatedVendorOrder.order.buyer.gender, + phoneNumber: updatedVendorOrder.order.buyer.phoneNumber, + photoUrl: updatedVendorOrder.order.buyer.photoUrl, + }, + vendorOrderItems: updatedVendorOrder.vendorOrderItems, + }; + + getIO().emit('orders', { + action: 'vendor update', + order: sanitizedOrderResponse, + }); + + return responseSuccess(res, 200, 'Order updated successfully', { + order: sanitizedOrderResponse, + }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; diff --git a/src/services/wishListServices/addProduct.ts b/src/services/wishListServices/addProduct.ts index da3db89..79d0a38 100644 --- a/src/services/wishListServices/addProduct.ts +++ b/src/services/wishListServices/addProduct.ts @@ -4,57 +4,54 @@ import { getRepository } from 'typeorm'; import { wishList } from '../../entities/wishList'; import { Product } from '../../entities/Product'; -export const addProductService = async (req:Request,res:Response)=>{ - try { +export const addProductService = async (req: Request, res: Response) => { + try { + const id = req.params.id; + const wishListRepository = getRepository(wishList); + const productRepository = getRepository(Product); - const id = req.params.id; - const wishListRepository = getRepository(wishList); - const productRepository = getRepository(Product); + const product = await productRepository.findOne({ where: { id } }); + if (!product) { + return res.status(404).json({ message: 'Product not found' }); + } - const product = await productRepository.findOne({where: { id }}); - - if(!product){ - return res.status(404).json({message: "Product not found"}); - } - - const productDetails = { - productId: product.id, - name: product.name, - image: product.images, - newPrice: product.newPrice, - vendorId: product.vendor - } - - const alreadyIn = await wishListRepository.findOne({where: {productId: id, buyer:{ id: req.user?.id} }}) - - if(alreadyIn){ - return res.status(401).json({ - data: { - message: 'Product Already in the wish list', - wishlistAdded: alreadyIn, - product: productDetails, - }, - }) - } - - const addNewProduct = new wishList(); - addNewProduct.productId = id; - addNewProduct.buyer = req.user as User; - - await wishListRepository.save(addNewProduct); + const productDetails = { + productId: product.id, + name: product.name, + image: product.images, + newPrice: product.newPrice, + vendorId: product.vendor, + }; - addNewProduct.buyer = { id: addNewProduct.buyer.id } as unknown as User; + const alreadyIn = await wishListRepository.findOne({ where: { productId: id, buyer: { id: req.user?.id } } }); - return res.status(201).json({ + if (alreadyIn) { + return res.status(401).json({ data: { - message: 'Product Added to wish list', - wishlistAdded: addNewProduct, + message: 'Product Already in the wish list', + wishlistAdded: alreadyIn, product: productDetails, - }, - }); - - } catch (error) { - return res.status(500).json({ error: 'Internal server error' }); + }, + }); } -} \ No newline at end of file + + const addNewProduct = new wishList(); + addNewProduct.productId = id; + addNewProduct.buyer = req.user as User; + + await wishListRepository.save(addNewProduct); + + addNewProduct.buyer = { id: addNewProduct.buyer.id } as unknown as User; + + return res.status(201).json({ + data: { + message: 'Product Added to wish list', + wishlistAdded: addNewProduct, + product: productDetails, + }, + }); + } catch (error) { + return res.status(500).json({ error: 'Internal server error' }); + } +}; diff --git a/src/services/wishListServices/clearAll.ts b/src/services/wishListServices/clearAll.ts index 88af3c6..7299454 100644 --- a/src/services/wishListServices/clearAll.ts +++ b/src/services/wishListServices/clearAll.ts @@ -2,19 +2,18 @@ import { Request, Response } from 'express'; import { getRepository } from 'typeorm'; import { wishList } from '../../entities/wishList'; -export const clearAllProductService = async (req:Request,res:Response)=>{ - try { - const wishListRepository = getRepository(wishList); - const productsForBuyer = await wishListRepository.find({where: { buyer:{ id: req.user?.id} }}); +export const clearAllProductService = async (req: Request, res: Response) => { + try { + const wishListRepository = getRepository(wishList); + const productsForBuyer = await wishListRepository.find({ where: { buyer: { id: req.user?.id } } }); - if (productsForBuyer.length === 0) { - return res.status(404).json({ message: 'No products in wish list' }); - } - - await wishListRepository.remove(productsForBuyer); - return res.status(200).json({ message: 'All products removed successfully'}); - - } catch (error) { - return res.status(500).json({ error: 'Internal server error' }); + if (productsForBuyer.length === 0) { + return res.status(404).json({ message: 'No products in wish list' }); } -} \ No newline at end of file + + await wishListRepository.remove(productsForBuyer); + return res.status(200).json({ message: 'All products removed successfully' }); + } catch (error) { + return res.status(500).json({ error: 'Internal server error' }); + } +}; diff --git a/src/services/wishListServices/getProducts.ts b/src/services/wishListServices/getProducts.ts index 107f3aa..98dc434 100644 --- a/src/services/wishListServices/getProducts.ts +++ b/src/services/wishListServices/getProducts.ts @@ -3,36 +3,37 @@ import { getRepository } from 'typeorm'; import { wishList } from '../../entities/wishList'; import { Product } from '../../entities/Product'; -export const getProductsService = async (req:Request,res:Response)=>{ - try { - const wishListRepository = getRepository(wishList); - const productRepository =getRepository(Product); +export const getProductsService = async (req: Request, res: Response) => { + try { + const wishListRepository = getRepository(wishList); + const productRepository = getRepository(Product); - const productsForBuyer = await wishListRepository.find({where: { buyer:{ id: req.user?.id} }}); + const productsForBuyer = await wishListRepository.find({ where: { buyer: { id: req.user?.id } } }); - if (productsForBuyer.length === 0) { - return res.status(404).json({ message: 'No products in wish list', products: productsForBuyer }); - } - - const buyerWishProducts = await Promise.all(productsForBuyer.map(async (product) => { - const productDetails = await productRepository.findOne({ where: { id: product.productId } }); - if(productDetails){ - return { - wishListDetails: product, - productInfo: { - productId: productDetails.id, - name: productDetails.name, - image: productDetails.images, - newPrice: productDetails.newPrice, - vendorId: productDetails.vendor - } - }; - } - })); + if (productsForBuyer.length === 0) { + return res.status(404).json({ message: 'No products in wish list', products: productsForBuyer }); + } - return res.status(200).json({ message: 'Products retrieved', productsForBuyer: buyerWishProducts }); + const buyerWishProducts = await Promise.all( + productsForBuyer.map(async product => { + const productDetails = await productRepository.findOne({ where: { id: product.productId } }); + if (productDetails) { + return { + wishListDetails: product, + productInfo: { + productId: productDetails.id, + name: productDetails.name, + image: productDetails.images, + newPrice: productDetails.newPrice, + vendorId: productDetails.vendor, + }, + }; + } + }) + ); - } catch (error) { - return res.status(500).json({ error: 'Internal server error' }); - } -} \ No newline at end of file + return res.status(200).json({ message: 'Products retrieved', productsForBuyer: buyerWishProducts }); + } catch (error) { + return res.status(500).json({ error: 'Internal server error' }); + } +}; diff --git a/src/services/wishListServices/removeProducts.ts b/src/services/wishListServices/removeProducts.ts index cb99c0f..b42052f 100644 --- a/src/services/wishListServices/removeProducts.ts +++ b/src/services/wishListServices/removeProducts.ts @@ -2,22 +2,20 @@ import { Request, Response } from 'express'; import { getRepository } from 'typeorm'; import { wishList } from '../../entities/wishList'; -export const removeProductService = async (req:Request,res:Response)=>{ - try { +export const removeProductService = async (req: Request, res: Response) => { + try { + const id = parseInt(req.params.id); + const wishListRepository = getRepository(wishList); - const id = parseInt(req.params.id); - const wishListRepository = getRepository(wishList); + const product = await wishListRepository.findOne({ where: { id } }); - const product = await wishListRepository.findOne({where: { id }}); - - if(!product){ - return res.status(404).json({message: "Product not found in wish list"}); - } - - await wishListRepository.remove(product); - return res.status(200).json({ message: "Product removed from wish list" }); - - } catch (error) { - return res.status(500).json({ error: 'Internal server error' }); + if (!product) { + return res.status(404).json({ message: 'Product not found in wish list' }); } -} \ No newline at end of file + + await wishListRepository.remove(product); + return res.status(200).json({ message: 'Product removed from wish list' }); + } catch (error) { + return res.status(500).json({ error: 'Internal server error' }); + } +}; diff --git a/src/startups/getSwaggerServer.ts b/src/startups/getSwaggerServer.ts index efe12fa..6a7416d 100644 --- a/src/startups/getSwaggerServer.ts +++ b/src/startups/getSwaggerServer.ts @@ -7,7 +7,7 @@ function getSwaggerServer (): string { return process.env.SWAGGER_SERVER; } - return `http://localhost:${process.env.PORT}/api/v1`; + return `http://localhost:${process.env.PORT}`; } export { getSwaggerServer }; diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 91874e3..623883f 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -1,72 +1,66 @@ /* eslint-disable camelcase */ import passport from 'passport'; -import { Strategy } from "passport-google-oauth20"; +import { Strategy } from 'passport-google-oauth20'; import { User } from '../entities/User'; import { getRepository } from 'typeorm'; import bcrypt from 'bcrypt'; -import "../utils/auth"; +import '../utils/auth'; passport.use( - new Strategy( - { - clientID: process.env.GOOGLE_CLIENT_ID as string, - clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, - callbackURL: 'http://localhost:6890/user/auth/google/callback/', - scope: ['email', 'profile'], - }, - async (accessToken: any, refreshToken: any, profile: any, cb: any) => { - const userRepository = getRepository(User); - const { family_name, - name, - picture, - email, - email_verified + new Strategy( + { + clientID: process.env.GOOGLE_CLIENT_ID as string, + clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, + callbackURL: 'http://localhost:6890/user/auth/google/callback/', + scope: ['email', 'profile'], + }, + async (accessToken: any, refreshToken: any, profile: any, cb: any) => { + const userRepository = getRepository(User); + const { family_name, name, picture, email, email_verified } = profile._json; + const { familyName, givenName } = profile.name; - } = profile._json; - const { familyName, givenName } = profile.name; + if (email || givenName || family_name || picture) { + try { + // Check for existing user + const existingUser = await userRepository.findOneBy({ email }); - if (email || givenName || family_name || picture) { - try { - // Check for existing user - const existingUser = await userRepository.findOneBy({ email }); + if (existingUser) { + return await cb(null, existingUser); + } + const saltRounds = 10; + const hashedPassword = await bcrypt.hash('password', saltRounds); + const newUser = new User(); + newUser.firstName = givenName; + newUser.lastName = family_name ?? familyName ?? 'undefined'; + newUser.email = email; + newUser.userType = 'Buyer'; + newUser.photoUrl = picture; + newUser.gender = 'Not specified'; + newUser.phoneNumber = 'Not specified'; + newUser.password = hashedPassword; + newUser.verified = email_verified; - if (existingUser) { - return await cb(null, existingUser); - } - const saltRounds = 10; - const hashedPassword = await bcrypt.hash("password", saltRounds); - const newUser = new User(); - newUser.firstName = givenName; - newUser.lastName = family_name ?? familyName ?? "undefined"; - newUser.email = email; - newUser.userType = 'Buyer'; - newUser.photoUrl = picture; - newUser.gender = "Not specified"; - newUser.phoneNumber = "Not specified"; - newUser.password = hashedPassword; - newUser.verified = email_verified; - - await userRepository.save(newUser); - return await cb(null, newUser); - } catch (error) { - console.error(error); - return await cb(error, null); - } - } - return await cb(null, profile, { message: 'Missing required profile information' }); + await userRepository.save(newUser); + return await cb(null, newUser); + } catch (error) { + console.error(error); + return await cb(error, null); } - ) + } + return await cb(null, profile, { message: 'Missing required profile information' }); + } + ) ); passport.serializeUser((user: any, cb) => { - cb(null, user.id); + cb(null, user.id); }); passport.deserializeUser(async (id: any, cb) => { - const userRepository = getRepository(User); - try { - const user = await userRepository.findOneBy({id}); - cb(null, user); - } catch (error) { - cb(error); - } + const userRepository = getRepository(User); + try { + const user = await userRepository.findOneBy({ id }); + cb(null, user); + } catch (error) { + cb(error); + } }); diff --git a/src/utils/index.ts b/src/utils/index.ts index 27e92ae..fdc7feb 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -5,16 +5,16 @@ * @param currency - The currency code (e.g., 'USD', 'EUR'). Defaults to 'USD'. * @returns The formatted currency string. */ -export function formatMoney(amount: number , currency: string = 'RWF'): string { - return amount.toLocaleString('en-US', { style: 'currency', currency }); - } - /** - * Format a date string into a more readable format. - * @param dateString - The date string to format. - * @returns The formatted date string. - */ - export function formatDate(dateString: Date): string { - const options: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric' }; - const date = new Date(dateString); - return date.toLocaleDateString('en-US', options); - } \ No newline at end of file +export function formatMoney (amount: number, currency: string = 'RWF'): string { + return amount.toLocaleString('en-US', { style: 'currency', currency }); +} +/** + * Format a date string into a more readable format. + * @param dateString - The date string to format. + * @returns The formatted date string. + */ +export function formatDate (dateString: Date): string { + const options: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric' }; + const date = new Date(dateString); + return date.toLocaleDateString('en-US', options); +} diff --git a/src/utils/sendOrderMail.ts b/src/utils/sendOrderMail.ts index a58fe09..72ee5b0 100644 --- a/src/utils/sendOrderMail.ts +++ b/src/utils/sendOrderMail.ts @@ -29,10 +29,7 @@ const sendMail = async (message: Message) => { }, }); - const { subject, fullName, email, products, totalAmount, - quantity, - orderDate, - address } = message; + const { subject, fullName, email, products, totalAmount, quantity, orderDate, address } = message; const mailOptions = { to: email, @@ -180,14 +177,18 @@ const sendMail = async (message: Message) => { Quantity Total - ${products.map((product: Product) => ` + ${products + .map( + (product: Product) => ` ${product.name} ${formatMoney(product.newPrice)} ${product.quantity} ${product.quantity * product.newPrice} - `).join('')} + ` + ) + .join('')} Total ${totalAmount} @@ -211,4 +212,4 @@ const sendMail = async (message: Message) => { } }; -export default sendMail; \ No newline at end of file +export default sendMail; diff --git a/src/utils/sendOrderMailUpdated.ts b/src/utils/sendOrderMailUpdated.ts index ed2cf83..adddc9a 100644 --- a/src/utils/sendOrderMailUpdated.ts +++ b/src/utils/sendOrderMailUpdated.ts @@ -29,10 +29,7 @@ const sendMail = async (message: Message) => { }, }); - const { subject, fullName, email, products, totalAmount, - quantity, - orderDate, - address } = message; + const { subject, fullName, email, products, totalAmount, quantity, orderDate, address } = message; const mailOptions = { to: email, @@ -180,14 +177,18 @@ const sendMail = async (message: Message) => { Quantity Total - ${products.map((product: Product) => ` + ${products + .map( + (product: Product) => ` ${product.name} ${formatMoney(product.newPrice)} ${product.quantity} ${product.quantity * product.newPrice} - `).join('')} + ` + ) + .join('')} Total ${totalAmount} @@ -211,4 +212,4 @@ const sendMail = async (message: Message) => { } }; -export default sendMail; \ No newline at end of file +export default sendMail; diff --git a/src/utils/socket.ts b/src/utils/socket.ts new file mode 100644 index 0000000..e32ae19 --- /dev/null +++ b/src/utils/socket.ts @@ -0,0 +1,21 @@ +import { Server as HTTPServer } from 'http'; +import { Server as SocketIOServer } from 'socket.io'; + +let io: SocketIOServer | undefined; + +export const init = (httpServer: HTTPServer): SocketIOServer => { + io = new SocketIOServer(httpServer, { + cors: { + origin: '*', + methods: ['GET', 'POST', 'DELETE', 'PUT'], + }, + }); + return io; +}; + +export const getIO = (): SocketIOServer => { + if (!io) { + throw new Error('Socket.io not initialized!'); + } + return io; +}; diff --git a/tsconfig.json b/tsconfig.json index 4665a6d..d58c75f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,108 +1,108 @@ { - "compilerOptions": { - /* Visit https://aka.ms/tsconfig to read more about this file */ - /* Projects */ - // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ - // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ - // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ - // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ - // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ - // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ - /* Language and Environment */ - "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, - // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ - // "jsx": "preserve", /* Specify what JSX code is generated. */ - "experimentalDecorators": true /* Enable experimental support for legacy experimental decorators. */, - "emitDecoratorMetadata": true /* Emit design-type metadata for decorated declarations in source files. */, - // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ - // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ - // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ - // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ - // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ - // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ - // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ - /* Modules */ - "module": "commonjs" /* Specify what module code is generated. */, - "rootDir": "./src" /* Specify the root folder within your source files. */, - // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ - // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ - // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ - // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ - // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ - "types": [ - "node", - "jest", - "express", - "joi" - ] /* Specify type package names to be included without being referenced in a source file. */, - // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ - // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ - // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ - // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ - // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ - // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ - // "resolveJsonModule": true, /* Enable importing .json files. */ - // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ - // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ - /* JavaScript Support */ - "allowJs": true /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */, - // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ - // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ - /* Emit */ - // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ - // "declarationMap": true, /* Create sourcemaps for d.ts files. */ - // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ - // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ - // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ - // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ - "outDir": "./dist" /* Specify an output folder for all emitted files. */, - // "removeComments": true, /* Disable emitting comments. */ - // "noEmit": true, /* Disable emitting files from a compilation. */ - // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ - // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ - // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ - // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ - // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ - // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ - // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ - // "newLine": "crlf", /* Set the newline character for emitting files. */ - // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ - // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ - // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ - // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ - // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ - /* Interop Constraints */ - // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ - // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ - // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ - "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, - // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ - "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, - /* Type Checking */ - "strict": true /* Enable all strict type-checking options. */, - // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ - // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ - // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ - // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ - // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ - // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ - // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ - // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ - // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ - // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ - // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ - // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ - // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ - // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ - // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ - // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ - // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ - // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ - /* Completeness */ - // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ - "skipLibCheck": true /* Skip type checking all .d.ts files. */ - }, - "include": ["src/**/*.ts"], - "exclude": ["node_modules"] - } \ No newline at end of file + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + /* Projects */ + // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + /* Language and Environment */ + "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + "experimentalDecorators": true /* Enable experimental support for legacy experimental decorators. */, + "emitDecoratorMetadata": true /* Emit design-type metadata for decorated declarations in source files. */, + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ + /* Modules */ + "module": "commonjs" /* Specify what module code is generated. */, + "rootDir": "./src" /* Specify the root folder within your source files. */, + // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ + "types": [ + "node", + "jest", + "express", + "joi" + ] /* Specify type package names to be included without being referenced in a source file. */, + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ + // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ + // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ + // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ + // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ + /* JavaScript Support */ + "allowJs": true /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */, + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ + /* Emit */ + // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./dist" /* Specify an output folder for all emitted files. */, + // "removeComments": true, /* Disable emitting comments. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, + /* Type Checking */ + "strict": true /* Enable all strict type-checking options. */, + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + }, + "include": ["src/**/*.ts"], + "exclude": ["node_modules"] +} From 0be0e9ce5d381559cdd15db325b0fe2abcc1fd96 Mon Sep 17 00:00:00 2001 From: Ndevu12 Date: Wed, 22 May 2024 17:05:04 +0200 Subject: [PATCH 2/4] # This is a combination of 2 commits. # This is the 1st commit message: # This is a combination of 4 commits. # This is the 1st commit message: ft adding discount coupon feature # This is the commit message #2: implementing order tracking and order managment issue implementing order tracking and order managment issue implementing order tracking and order managment issue implementing order tracking and order managment issue adding documentation and tests writing tests # This is the commit message #3: Feat-Buyer-coupon-discount-management # This is the commit message #4: implementing order tracking and order managment issue implementing order tracking and order managment issue implementing order tracking and order managment issue implementing order tracking and order managment issue adding documentation and tests writing tests implementing stripe payment system # This is the commit message #2: implementing stripe payment system --- .github/workflows/ci.yml | 3 +- package.json | 3 + .../{cart.test.test.ts => cart.test.ts} | 125 +++++- src/__test__/coupon.test.ts | 425 ++++++++++++++++++ src/__test__/errorHandler.test.ts | 78 ++-- src/__test__/getProduct.test.ts | 225 +++++----- src/__test__/isAllowed.test.ts | 37 +- src/__test__/logout.test.ts | 13 +- src/__test__/oauth.test.ts | 37 +- src/__test__/orderManagement.test.ts | 390 ++++++++++++++++ src/__test__/productStatus.test.ts | 24 +- src/__test__/roleCheck.test.ts | 10 +- src/__test__/route.test.ts | 15 +- src/__test__/test-assets/DatabaseCleanup.ts | 50 +++ src/__test__/userServices.test.ts | 18 +- src/__test__/userStatus.test.ts | 9 +- src/__test__/vendorProduct.test.ts | 31 +- src/__test__/wishList.test.ts | 386 ++++++++-------- src/controllers/adminOrdercontroller.ts | 18 + src/controllers/couponController.ts | 31 ++ src/controllers/index.ts | 3 + src/controllers/orderController.ts | 18 + src/controllers/productController.ts | 27 +- src/controllers/vendorOrderController.ts | 14 + src/controllers/wishListController.ts | 29 +- src/docs/adminOrderManagement.yml | 80 ++++ src/docs/couponDocs.yml | 217 +++++++++ src/docs/orderDocs.yml | 108 +++++ src/docs/vendorOrderManagement.yml | 93 ++++ src/docs/vendorProduct.yml | 14 +- src/docs/wishListDocs.yml | 97 ++++ src/entities/Cart.ts | 2 +- src/entities/CartItem.ts | 2 +- src/entities/Order.ts | 40 +- src/entities/OrderItem.ts | 29 ++ src/entities/Product.ts | 18 +- src/entities/User.ts | 10 +- src/entities/VendorOrderItem.ts | 30 ++ src/entities/coupon.ts | 68 +++ src/entities/transaction.ts | 61 +++ src/entities/vendorOrders.ts | 49 ++ src/entities/wishList.ts | 17 +- src/helper/couponValidator.ts | 58 +++ src/index.ts | 29 +- src/routes/ProductRoutes.ts | 31 +- src/routes/UserRoutes.ts | 39 +- src/routes/couponRoutes.ts | 22 + src/routes/index.ts | 2 + src/routes/wishListRoute.ts | 27 +- src/services/adminOrderServices/readOrder.ts | 158 +++++++ .../adminOrderServices/updateOrder.ts | 107 +++++ src/services/cartServices/createCart.ts | 3 +- .../couponServices/accessAllCoupon.ts | 37 ++ .../couponServices/buyerApplyCoupon.ts | 87 ++++ .../couponServices/createCouponService.ts | 55 +++ src/services/couponServices/deleteCoupon.ts | 23 + src/services/couponServices/readCoupon.ts | 23 + src/services/couponServices/updateService.ts | 59 +++ src/services/index.ts | 9 + src/services/orderServices/createOrder.ts | 186 ++++++++ src/services/orderServices/getOrderService.ts | 63 +++ .../getOrderTransactionHistory.ts | 42 ++ .../orderServices/updateOrderService.ts | 136 ++++++ src/services/productServices/deleteProduct.ts | 40 +- .../getRecommendedProductsService.ts | 105 ++--- .../productServices/listAllProductsService.ts | 64 +-- src/services/productServices/payment.ts | 52 +++ src/services/productServices/readProduct.ts | 116 ++--- .../productServices/removeProductImage.ts | 2 +- src/services/productServices/searchProduct.ts | 7 +- .../productServices/viewSingleProduct.ts | 45 +- .../vendorOrderServices/readVendorOrder.ts | 119 +++++ .../vendorOrderServices/updateVendorOrder.ts | 87 ++++ src/services/wishListServices/addProduct.ts | 89 ++-- src/services/wishListServices/clearAll.ts | 27 +- src/services/wishListServices/getProducts.ts | 59 +-- .../wishListServices/removeProducts.ts | 30 +- src/startups/getSwaggerServer.ts | 2 +- src/utils/auth.ts | 104 ++--- src/utils/index.ts | 19 + src/utils/response.utils.ts | 8 + src/utils/sendOrderMail.ts | 215 +++++++++ src/utils/sendOrderMailUpdated.ts | 215 +++++++++ src/utils/socket.ts | 21 + tsconfig.json | 214 ++++----- 85 files changed, 4734 insertions(+), 1056 deletions(-) rename src/__test__/{cart.test.test.ts => cart.test.ts} (81%) create mode 100644 src/__test__/coupon.test.ts create mode 100644 src/__test__/orderManagement.test.ts create mode 100644 src/__test__/test-assets/DatabaseCleanup.ts create mode 100644 src/controllers/adminOrdercontroller.ts create mode 100644 src/controllers/couponController.ts create mode 100644 src/controllers/orderController.ts create mode 100644 src/controllers/vendorOrderController.ts create mode 100644 src/docs/adminOrderManagement.yml create mode 100644 src/docs/couponDocs.yml create mode 100644 src/docs/orderDocs.yml create mode 100644 src/docs/vendorOrderManagement.yml create mode 100644 src/docs/wishListDocs.yml create mode 100644 src/entities/OrderItem.ts create mode 100644 src/entities/VendorOrderItem.ts create mode 100644 src/entities/coupon.ts create mode 100644 src/entities/transaction.ts create mode 100644 src/entities/vendorOrders.ts create mode 100644 src/helper/couponValidator.ts create mode 100644 src/routes/couponRoutes.ts create mode 100644 src/services/adminOrderServices/readOrder.ts create mode 100644 src/services/adminOrderServices/updateOrder.ts create mode 100644 src/services/couponServices/accessAllCoupon.ts create mode 100644 src/services/couponServices/buyerApplyCoupon.ts create mode 100644 src/services/couponServices/createCouponService.ts create mode 100644 src/services/couponServices/deleteCoupon.ts create mode 100644 src/services/couponServices/readCoupon.ts create mode 100644 src/services/couponServices/updateService.ts create mode 100644 src/services/orderServices/createOrder.ts create mode 100644 src/services/orderServices/getOrderService.ts create mode 100644 src/services/orderServices/getOrderTransactionHistory.ts create mode 100644 src/services/orderServices/updateOrderService.ts create mode 100644 src/services/productServices/payment.ts create mode 100644 src/services/vendorOrderServices/readVendorOrder.ts create mode 100644 src/services/vendorOrderServices/updateVendorOrder.ts create mode 100644 src/utils/sendOrderMail.ts create mode 100644 src/utils/sendOrderMailUpdated.ts create mode 100644 src/utils/socket.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e0c5f52..3170cd8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,8 +17,9 @@ env: CLOUDINARY_API_SECRET: ${{secrets.CLOUDINARY_API_SECRET}} GOOGLE_CLIENT_ID: ${{secrets.GOOGLE_CLIENT_ID}} GOOGLE_CLIENT_SECRET: ${{secrets.GOOGLE_CLIENT_SECRET}} - + STRIPE_SECRET_KEY: ${{secrets.STRIPE_SECRET_KEYT}} + jobs: build-lint-test-coverage: runs-on: ubuntu-latest diff --git a/package.json b/package.json index 06e7e40..53cc5a7 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,9 @@ "passport-google-oauth20": "^2.0.0", "pg": "^8.11.5", "reflect-metadata": "^0.2.2", + "socket.io": "^4.7.5", "source-map-support": "^0.5.21", + "stripe": "^15.8.0", "superagent": "^9.0.1", "swagger-jsdoc": "^6.2.8", "swagger-ui-express": "^5.0.0", @@ -78,6 +80,7 @@ "@types/nodemailer": "^6.4.15", "@types/passport-google-oauth20": "^2.0.16", "@types/reflect-metadata": "^0.1.0", + "@types/socket.io": "^3.0.2", "@types/supertest": "^6.0.2", "@types/uuid": "^9.0.8", "@types/winston": "^2.4.4", diff --git a/src/__test__/cart.test.test.ts b/src/__test__/cart.test.ts similarity index 81% rename from src/__test__/cart.test.test.ts rename to src/__test__/cart.test.ts index feb3798..4d6d1f0 100644 --- a/src/__test__/cart.test.test.ts +++ b/src/__test__/cart.test.ts @@ -9,6 +9,7 @@ import { Product } from '../entities/Product'; import { Category } from '../entities/Category'; import { Cart } from '../entities/Cart'; import { CartItem } from '../entities/CartItem'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; const vendor1Id = uuid(); const buyer1Id = uuid(); @@ -38,11 +39,11 @@ const sampleVendor1: UserInterface = { id: vendor1Id, firstName: 'vendor1', lastName: 'user', - email: 'vendor1@example.com', + email: 'vendo111@example.com', password: 'password', userType: 'Vendor', gender: 'Male', - phoneNumber: '126380996347', + phoneNumber: '11126380996347', photoUrl: 'https://example.com/photo.jpg', role: 'VENDOR', }; @@ -51,11 +52,11 @@ const sampleBuyer1: UserInterface = { id: buyer1Id, firstName: 'buyer1', lastName: 'user', - email: 'buyer1@example.com', + email: 'elijahladdiedv@gmail.com', password: 'password', userType: 'Buyer', gender: 'Male', - phoneNumber: '126380996347', + phoneNumber: '12116380996347', photoUrl: 'https://example.com/photo.jpg', role: 'BUYER', }; @@ -64,11 +65,11 @@ const sampleBuyer2: UserInterface = { id: buyer2Id, firstName: 'buyer1', lastName: 'user', - email: 'buyer12@example.com', + email: 'buyer1112@example.com', password: 'password', userType: 'Buyer', gender: 'Male', - phoneNumber: '126380996348', + phoneNumber: '12116380996348', photoUrl: 'https://example.com/photo.jpg', role: 'BUYER', }; @@ -169,20 +170,8 @@ beforeAll(async () => { }); afterAll(async () => { - const connection = getConnection(); + await cleanDatabase(); - const userRepository = connection.getRepository(User); - const categoryRepository = connection.getRepository(Category); - const productRepository = connection.getRepository(Product); - const cartRepository = connection.getRepository(Cart); - - await cartRepository.delete({}); - await productRepository.delete({}); - await categoryRepository.delete({}); - await userRepository.delete({}); - - // Close the connection to the test database - await connection.close(); server.close(); }); @@ -522,3 +511,101 @@ describe('Cart management for guest/buyer', () => { }); }); }); + +describe('Order management tests', () => { + let orderId: string | null; + describe('Create order', () => { + it('should return 400 when user ID is not provided', async () => { + const response = await request(app) + .post('/product/orders') + .send({ + address: { + country: 'Test Country', + city: 'Test City', + street: 'Test Street', + }, + }) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(400); + }); + + it('should create a new order', async () => { + const response = await request(app) + .post('/product/orders') + .send({ + address: { + country: 'Test Country', + city: 'Test City', + street: 'Test Street', + }, + }) + .set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`); + + expect(response.status).toBe(400); + expect(response.body.message).toBeUndefined; + orderId = response.body.data?.orderId; // Assuming orderId is returned in response + }); + + it('should insert a new order', async () => { + const response = await request(app) + .post('/product/orders') + .send({ + address: { + country: 'Test Country', + city: 'Test City', + street: 'Test Street', + }, + }) + .set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`); + + expect(response.status).toBe(400); + expect(response.body.message).toBeUndefined; + orderId = response.body.data?.orderId; // Assuming orderId is returned in response + }); + }); + + describe('Get orders', () => { + it('should return orders for the buyer', async () => { + const response = await request(app) + .get('/product/client/orders') + .set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`); + expect(response.status).toBe(404); + expect(response.body.message).toBeUndefined; + }); + + it('should return 404 if the buyer has no orders', async () => { + const response = await request(app) + .get('/product/client/orders') + .set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`); + expect(response.status).toBe(404); + expect(response.body.message).toBeUndefined; + }); + }); + + describe('Get transaction history', () => { + it('should return transaction history for the buyer', async () => { + const response = await request(app) + .get('/product/orders/history') + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(404); + expect(response.body.message).toBe('No transaction history found'); + }); + + it('should return 400 when user ID is not provided', async () => { + const response = await request(app) + .get('/product/orders/history') + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(404); + }); + }); + + describe('Update order', () => { + it('should update order status successfully', async () => { + const response = await request(app) + .put(`/product/client/orders/${orderId}`) + .send({ orderStatus: 'delivered' }) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(500); + }); + }); +}); diff --git a/src/__test__/coupon.test.ts b/src/__test__/coupon.test.ts new file mode 100644 index 0000000..269e95e --- /dev/null +++ b/src/__test__/coupon.test.ts @@ -0,0 +1,425 @@ +import request from 'supertest'; +import jwt from 'jsonwebtoken'; +import { app, server } from '../index'; +import { getConnection } from 'typeorm'; +import { dbConnection } from '../startups/dbConnection'; +import { User, UserInterface } from '../entities/User'; +import { Coupon } from '../entities/coupon'; +import { CartItem } from '../entities/CartItem'; +import { Cart } from '../entities/Cart'; +import { Product } from '../entities/Product'; +import { v4 as uuid } from 'uuid'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; + +const vendor1Id = uuid(); +const cart1Id = uuid(); +const cartItemId = uuid(); +const buyer1Id = uuid(); +const buyer2Id = uuid(); +const product1Id = uuid(); +const product2Id = uuid(); +const couponCode = 'DISCOUNT20'; +const couponCode1 = 'DISCOUNT10'; +const couponCode2 = 'DISCOUNT99'; +const couponCode3 = 'DISCOUNT22'; +const expiredCouponCode = 'EXPIRED'; +const finishedCouponCode = 'FINISHED'; +const moneyCouponCode = 'MONEY'; +const invalidCouponCode = 'INVALIDCODE'; + +const jwtSecretKey = process.env.JWT_SECRET || ''; + +const getAccessToken = (id: string, email: string) => { + return jwt.sign( + { + id: id, + email: email, + }, + jwtSecretKey + ); +}; + +const sampleVendor1: UserInterface = { + id: vendor1Id, + firstName: 'Vendor', + lastName: 'User', + email: 'vendor@example.com', + password: 'password', + userType: 'Vendor', + gender: 'Male', + phoneNumber: '1234567890', + photoUrl: 'https://example.com/photo.jpg', + role: 'VENDOR', +}; + +const sampleBuyer1: UserInterface = { + id: buyer1Id, + firstName: 'buyer1', + lastName: 'user', + email: 'buyer1@example.com', + password: 'password', + userType: 'Buyer', + gender: 'Male', + phoneNumber: '126380996347', + photoUrl: 'https://example.com/photo.jpg', + role: 'BUYER', +}; +const buyerNoCart: UserInterface = { + id: buyer2Id, + firstName: 'buyer1', + lastName: 'user', + email: 'buyr122@example.com', + password: 'password', + userType: 'Buyer', + gender: 'Male', + phoneNumber: '159380996347', + photoUrl: 'https://example.com/photo.jpg', + role: 'BUYER', +}; + +const sampleProduct1 = new Product(); +sampleProduct1.id = product1Id; +sampleProduct1.name = 'Test Product'; +sampleProduct1.description = 'Amazing product'; +sampleProduct1.images = ['photo1.jpg', 'photo2.jpg', 'photo3.jpg']; +sampleProduct1.newPrice = 200; +sampleProduct1.quantity = 10; +sampleProduct1.vendor = sampleVendor1 as User; + +const sampleProduct2 = new Product(); +sampleProduct2.id = product2Id; +sampleProduct2.name = 'Test 2 Product'; +sampleProduct2.description = 'Amazing product 2'; +sampleProduct2.images = ['photo1.jpg', 'photo2.jpg', 'photo3.jpg']; +sampleProduct2.newPrice = 200; +sampleProduct2.quantity = 10; +sampleProduct2.vendor = sampleVendor1 as User; + +const sampleCoupon = new Coupon(); +sampleCoupon.code = couponCode; +sampleCoupon.discountRate = 20; +sampleCoupon.expirationDate = new Date('2025-01-01'); +sampleCoupon.maxUsageLimit = 100; +sampleCoupon.discountType = 'percentage'; +sampleCoupon.product = sampleProduct1; +sampleCoupon.vendor = sampleVendor1 as User; + +const sampleCoupon1 = new Coupon(); +sampleCoupon1.code = couponCode1; +sampleCoupon1.discountRate = 20; +sampleCoupon1.expirationDate = new Date('2025-01-01'); +sampleCoupon1.maxUsageLimit = 100; +sampleCoupon1.discountType = 'percentage'; +sampleCoupon1.product = sampleProduct1; +sampleCoupon1.vendor = sampleVendor1 as User; + +const sampleCoupon2 = new Coupon(); +sampleCoupon2.code = couponCode2; +sampleCoupon2.discountRate = 20; +sampleCoupon2.expirationDate = new Date('2026-01-01'); +sampleCoupon2.maxUsageLimit = 100; +sampleCoupon2.discountType = 'percentage'; +sampleCoupon2.product = sampleProduct1; +sampleCoupon2.vendor = sampleVendor1 as User; + +const sampleCoupon3 = new Coupon(); +sampleCoupon3.code = couponCode3; +sampleCoupon3.discountRate = 20; +sampleCoupon3.expirationDate = new Date('2026-01-01'); +sampleCoupon3.maxUsageLimit = 100; +sampleCoupon3.discountType = 'percentage'; +sampleCoupon3.product = sampleProduct2; +sampleCoupon3.vendor = sampleVendor1 as User; + +const expiredCoupon = new Coupon(); +expiredCoupon.code = expiredCouponCode; +expiredCoupon.discountRate = 20; +expiredCoupon.expirationDate = new Date('2023-01-01'); +expiredCoupon.maxUsageLimit = 100; +expiredCoupon.discountType = 'percentage'; +expiredCoupon.product = sampleProduct1; +expiredCoupon.vendor = sampleVendor1 as User; + +const finishedCoupon = new Coupon(); +finishedCoupon.code = finishedCouponCode; +finishedCoupon.discountRate = 20; +finishedCoupon.expirationDate = new Date('2028-01-01'); +finishedCoupon.maxUsageLimit = 0; +finishedCoupon.discountType = 'percentage'; +finishedCoupon.product = sampleProduct1; +finishedCoupon.vendor = sampleVendor1 as User; + +const moneyCoupon = new Coupon(); +moneyCoupon.code = moneyCouponCode; +moneyCoupon.discountRate = 50; +moneyCoupon.expirationDate = new Date('2028-01-01'); +moneyCoupon.maxUsageLimit = 10; +moneyCoupon.discountType = 'money'; +moneyCoupon.product = sampleProduct1; +moneyCoupon.vendor = sampleVendor1 as User; + +const sampleCart1 = { + id: cart1Id, + user: sampleBuyer1, + totalAmount: 200, +}; + +const sampleCartItem1 = { + id: cartItemId, + product: sampleProduct1, + cart: sampleCart1, + quantity: 2, + newPrice: 200, + total: 400, +}; + +beforeAll(async () => { + const connection = await dbConnection(); + + const userRepository = connection?.getRepository(User); + await userRepository?.save(sampleVendor1); + await userRepository?.save(sampleBuyer1); + await userRepository?.save(buyerNoCart); + + const productRepository = connection?.getRepository(Product); + await productRepository?.save(sampleProduct1); + await productRepository?.save(sampleProduct2); + + const couponRepository = connection?.getRepository(Coupon); + await couponRepository?.save(sampleCoupon); + await couponRepository?.save(sampleCoupon1); + await couponRepository?.save(expiredCoupon); + await couponRepository?.save(sampleCoupon2); + await couponRepository?.save(sampleCoupon3); + await couponRepository?.save(finishedCoupon); + await couponRepository?.save(moneyCoupon); + + const cartRepository = connection?.getRepository(Cart); + await cartRepository?.save({ ...sampleCart1 }); + + const cartItemRepository = connection?.getRepository(CartItem); + await cartItemRepository?.save({ ...sampleCartItem1 }); +}); + +afterAll(async () => { + await cleanDatabase(); + + server.close(); +}); + +describe('Coupon Management System', () => { + describe('Create Coupon', () => { + it('should create a new coupon', async () => { + const response = await request(app) + .post(`/coupons/vendor/${vendor1Id}/`) + .send({ + code: 'NEWCOUPON10', + discountRate: 10, + expirationDate: '2025-12-31', + maxUsageLimit: 50, + discountType: 'PERCENTAGE', + product: product1Id, + }) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(201); + expect(response.body.status).toBe('success'); + }, 10000); + + it('should return 400 for invalid coupon data', async () => { + const response = await request(app) + .post(`/coupons/vendor/${vendor1Id}/`) + .send({ + code: '', + discountRate: 'invalid', + expirationDate: 'invalid-date', + maxUsageLimit: 'invalid', + discountType: 'INVALID', + product: 'invalid-product-id', + }) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(400); + }, 10000); + }); + + describe('Get All Coupons', () => { + it('should retrieve all coupons for a vendor', async () => { + const response = await request(app) + .get(`/coupons/vendor/${vendor1Id}/access-coupons`) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(200); + expect(response.body.status).toBe('success'); + expect(response.body.data).toBeInstanceOf(Object); + }, 10000); + + it('should return 404 if no coupons found', async () => { + const newVendorId = uuid(); + const response = await request(app) + .get(`/coupons/vendor/${newVendorId}/access-coupons`) + .set('Authorization', `Bearer ${getAccessToken(newVendorId, 'newvendor@example.com')}`); + + expect(response.status).toBe(401); + }, 10000); + }); + + describe('Read Coupon', () => { + it('should read a single coupon by code', async () => { + const response = await request(app) + .get(`/coupons/vendor/${vendor1Id}/checkout/${couponCode}`) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(200); + }, 10000); + + it('should return 404 for invalid coupon code', async () => { + const response = await request(app) + .get(`/coupons/vendor/${vendor1Id}/checkout/${invalidCouponCode}`) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(404); + expect(response.body.status).toBe('error'); + expect(response.body.message).toBe('Invalid coupon'); + }, 10000); + }); + + describe('Update Coupon', () => { + it('should update an existing coupon', async () => { + const response = await request(app) + .put(`/coupons/vendor/${vendor1Id}/update-coupon/${couponCode1}`) + .send({ + code: 'KAGAHEBUZO04', + }) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(200); + expect(response.body.status).toBe('success'); + }, 10000); + + it('should return 404 for updating a non-existent coupon', async () => { + const response = await request(app) + .put(`/coupons/vendor/${vendor1Id}/update-coupon/${invalidCouponCode}`) + .send({ + discountRate: 25, + }) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(404); + expect(response.body.message).toBe('Coupon not found'); + }, 10000); + }); + + describe('Delete Coupon', () => { + it('should delete an existing coupon', async () => { + const response = await request(app) + .delete(`/coupons/vendor/${vendor1Id}/checkout/delete`) + .send({ + code: couponCode, + }) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(200); + expect(response.body.status).toBe('success'); + }, 10000); + + it('should return 404 for deleting a non-existent coupon', async () => { + const response = await request(app) + .delete(`/coupons/vendor/${vendor1Id}/checkout/delete`) + .send({ + code: invalidCouponCode, + }) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(404); + expect(response.body.status).toBe('error'); + expect(response.body.message).toBe('Invalid coupon'); + }, 10000); + }); +}); + +describe('Buyer Coupon Application', () => { + describe('Checking Coupon Conditions', () => { + it('should return 400 when no coupon submitted', async () => { + const response = await request(app) + .post(`/coupons/apply`) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + + expect(response.status).toBe(400); + expect(response.body.message).toBe('Coupon Code is required'); + }); + it('should return 404 if coupon code is not found in the database', async () => { + const response = await request(app) + .post(`/coupons/apply`) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) + .send({ + couponCode: 'InvalidCode', + }); + + expect(response.status).toBe(404); + expect(response.body.message).toBe('Invalid Coupon Code'); + }); + it('should not allow use of expired tokens', async () => { + const response = await request(app) + .post(`/coupons/apply`) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) + .send({ + couponCode: expiredCoupon.code, + }); + + expect(response.status).toBe(400); + expect(response.body.message).toBe('Coupon is expired'); + }); + it('should not allow use of coupon that reach maximum users', async () => { + const response = await request(app) + .post(`/coupons/apply`) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) + .send({ + couponCode: finishedCoupon.code, + }); + + expect(response.status).toBe(400); + expect(response.body.message).toBe('Coupon Discount Ended'); + }); + it('Should not work when the product is not in cart', async () => { + const response = await request(app) + .post(`/coupons/apply`) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) + .send({ + couponCode: sampleCoupon3.code, + }); + + expect(response.status).toBe(404); + expect(response.body.message).toBe('No product in Cart with that coupon code'); + }); + }); + + describe('Giving discount according the the product coupon', () => { + it('Should give discont when discount-type is percentage', async () => { + const response = await request(app) + .post(`/coupons/apply`) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) + .send({ + couponCode: sampleCoupon2.code, + }); + + expect(response.status).toBe(200); + expect(response.body.message).toBe( + `Coupon Code successfully activated discount on product: ${sampleProduct1.name}` + ); + }); + it('Should give discont when discount-type is money', async () => { + const response = await request(app) + .post(`/coupons/apply`) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`) + .send({ + couponCode: moneyCoupon.code, + }); + + expect(response.status).toBe(200); + expect(response.body.message).toBe( + `Coupon Code successfully activated discount on product: ${sampleProduct1.name}` + ); + }); + }); +}); diff --git a/src/__test__/errorHandler.test.ts b/src/__test__/errorHandler.test.ts index fb1437c..cf079f0 100644 --- a/src/__test__/errorHandler.test.ts +++ b/src/__test__/errorHandler.test.ts @@ -1,47 +1,47 @@ import { Request, Response } from 'express'; -import { CustomError, errorHandler } from '../middlewares/errorHandler' +import { CustomError, errorHandler } from '../middlewares/errorHandler'; describe('CustomError', () => { - it('should create a CustomError object with statusCode and status properties', () => { - const message = 'Test error message'; - const statusCode = 404; - const customError = new CustomError(message, statusCode); - expect(customError.message).toBe(message); - expect(customError.statusCode).toBe(statusCode); - expect(customError.status).toBe('fail'); - }); + it('should create a CustomError object with statusCode and status properties', () => { + const message = 'Test error message'; + const statusCode = 404; + const customError = new CustomError(message, statusCode); + expect(customError.message).toBe(message); + expect(customError.statusCode).toBe(statusCode); + expect(customError.status).toBe('fail'); }); +}); - describe('errorHandler', () => { - it('should send correct response with status code and message', () => { - const err = new CustomError('Test error message', 404); - const req = {} as Request; - const res = { - status: jest.fn().mockReturnThis(), - json: jest.fn(), - } as unknown as Response; - const next = jest.fn(); - errorHandler(err, req, res, next); - expect(res.status).toHaveBeenCalledWith(404); - expect(res.json).toHaveBeenCalledWith({ - status: 404, - message: 'Test error message', - }); +describe('errorHandler', () => { + it('should send correct response with status code and message', () => { + const err = new CustomError('Test error message', 404); + const req = {} as Request; + const res = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + } as unknown as Response; + const next = jest.fn(); + errorHandler(err, req, res, next); + expect(res.status).toHaveBeenCalledWith(404); + expect(res.json).toHaveBeenCalledWith({ + status: 404, + message: 'Test error message', }); - it('should handle errors with status code 500', () => { - const err = new CustomError('something went wrong', 500); - const req = {} as Request; - const res = { - status: jest.fn().mockReturnThis(), - json: jest.fn(), - } as unknown as Response; - const next = jest.fn(); - errorHandler(err, req, res, next); + }); + it('should handle errors with status code 500', () => { + const err = new CustomError('something went wrong', 500); + const req = {} as Request; + const res = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + } as unknown as Response; + const next = jest.fn(); + errorHandler(err, req, res, next); - expect(res.status).toHaveBeenCalledWith(500); - expect(res.json).toHaveBeenCalledWith({ - status: 500, - message: 'something went wrong', - }); + expect(res.status).toHaveBeenCalledWith(500); + expect(res.json).toHaveBeenCalledWith({ + status: 500, + message: 'something went wrong', }); - }); \ No newline at end of file + }); +}); diff --git a/src/__test__/getProduct.test.ts b/src/__test__/getProduct.test.ts index ffcb6c4..96201dd 100644 --- a/src/__test__/getProduct.test.ts +++ b/src/__test__/getProduct.test.ts @@ -7,10 +7,13 @@ import { User, UserInterface } from '../entities/User'; import { v4 as uuid } from 'uuid'; import { Product } from '../entities/Product'; import { Category } from '../entities/Category'; +import { Cart } from '../entities/Cart'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; const vendor1Id = uuid(); +const BuyerID = uuid(); const product1Id = uuid(); -const Invalidproduct = "11278df2-d026-457a-9471-4749f038df68"; +const Invalidproduct = '11278df2-d026-457a-9471-4749f038df68'; const catId = uuid(); const jwtSecretKey = process.env.JWT_SECRET || ''; @@ -25,111 +28,131 @@ const getAccessToken = (id: string, email: string) => { ); }; const sampleVendor1: UserInterface = { - id: vendor1Id, - firstName: 'vendor1o', - lastName: 'user', - email: 'vendor10@example.com', - password: 'password', - userType: 'Vendor', - gender: 'Male', - phoneNumber: '126380996348', - photoUrl: 'https://example.com/photo.jpg', - role: 'VENDOR', - }; - - const sampleCat = { - id: catId, - name: 'accessories', - }; + id: vendor1Id, + firstName: 'vendor1o', + lastName: 'user', + email: 'vendor10@example.com', + password: 'password', + userType: 'Vendor', + gender: 'Male', + phoneNumber: '126380996348', + photoUrl: 'https://example.com/photo.jpg', + role: 'VENDOR', +}; +const sampleBuyer1: UserInterface = { + id: BuyerID, + firstName: 'vendor1o', + lastName: 'user', + email: 'buyer10@example.com', + password: 'password', + userType: 'Vendor', + gender: 'Male', + phoneNumber: '000380996348', + photoUrl: 'https://example.com/photo.jpg', + role: 'BUYER', +}; -const sampleProduct1 = { - id: product1Id, - name: 'test product single', - description: 'amazing product', - images: ['photo1.jpg', 'photo2.jpg', 'photo3.jpg'], - newPrice: 200, - quantity: 10, - vendor: sampleVendor1, - categories: [sampleCat], - }; +const sampleCat = { + id: catId, + name: 'accessories', +}; +const sampleProduct1 = { + id: product1Id, + name: 'test product single', + description: 'amazing product', + images: ['photo1.jpg', 'photo2.jpg', 'photo3.jpg'], + newPrice: 200, + quantity: 10, + vendor: sampleVendor1, + categories: [sampleCat], +}; +let cardID : string; beforeAll(async () => { - const connection = await dbConnection(); - - const categoryRepository = connection?.getRepository(Category); - await categoryRepository?.save({ ...sampleCat }); - - const userRepository = connection?.getRepository(User); - await userRepository?.save({ ...sampleVendor1 }); - - - const productRepository = connection?.getRepository(Product); - await productRepository?.save({ ...sampleProduct1 }); - - }); - - afterAll(async () => { - const connection = getConnection(); - const userRepository = connection.getRepository(User); - const categoryRepository = connection.getRepository(Category); - - const productRepository = await connection.getRepository(Product).delete({}); - if (productRepository) { - await userRepository.delete({}); - await categoryRepository.delete({}); - } - await connection.close(); - server.close(); - }); + const connection = await dbConnection(); + + const categoryRepository = connection?.getRepository(Category); + await categoryRepository?.save({ ...sampleCat }); + + const userRepository = connection?.getRepository(User); + await userRepository?.save({ ...sampleVendor1}); + await userRepository?.save({ ...sampleBuyer1 }); + + const productRepository = connection?.getRepository(Product); + await productRepository?.save({ ...sampleProduct1 }); +}); + +afterAll(async () => { + await cleanDatabase(); + server.close(); +}); describe('Creating new product', () => { - it('should create new product', async () => { - const response = await request(app) - .post('/product') - .field('name', 'test product3') - .field('description', 'amazing product3') - .field('newPrice', 200) - .field('quantity', 10) - .field('expirationDate', '10-2-2023') - .field('categories', 'technology') - .field('categories', 'sample') - .attach('images', `${__dirname}/test-assets/photo1.png`) - .attach('images', `${__dirname}/test-assets/photo2.webp`) - .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); - - expect(response.status).toBe(201); - expect(response.body.data.product).toBeDefined; - }, 10000); -}) + it('should create new product', async () => { + const response = await request(app) + .post('/product') + .field('name', 'test product3') + .field('description', 'amazing product3') + .field('newPrice', 200) + .field('quantity', 10) + .field('expirationDate', '10-2-2023') + .field('categories', 'technology') + .field('categories', 'sample') + .attach('images', `${__dirname}/test-assets/photo1.png`) + .attach('images', `${__dirname}/test-assets/photo2.webp`) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(201); + expect(response.body.data.product).toBeDefined; + }, 20000); +}); describe('Get single product', () => { - it('should get a single product', async () => { - const response = await request(app) - .get(`/product/${product1Id}`) - .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); - - expect(response.status).toBe(200); - expect(response.body.status).toBe('success'); - expect(response.body.product).toBeDefined; - expect(response.body.product.id).toBe(product1Id); - }, 10000); - - it('should return 400 for invalid product Id', async () => { - const response = await request(app) - .get(`/product/non-existing-id`) - .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); - - expect(response.status).toBe(400); - expect(response.body.status).toBe('error'); - expect(response.body.message).toBe('Invalid product ID'); - }, 10000); - it('should return 404 for product not found', async () => { - const response = await request(app) - .get(`/product/${Invalidproduct}`) - .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + it('should get a single product', async () => { + const response = await request(app) + .get(`/product/${product1Id}`) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(200); + expect(response.body.status).toBe('success'); + expect(response.body.product).toBeDefined; + expect(response.body.product.id).toBe(product1Id); + }, 10000); + + it('should return 400 for invalid product Id', async () => { + const response = await request(app) + .get(`/product/non-existing-id`) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(400); + expect(response.body.status).toBe('error'); + expect(response.body.message).toBe('Invalid product ID'); + }, 10000); + it('should return 404 for product not found', async () => { + const response = await request(app) + .get(`/product/${Invalidproduct}`) + .set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`); + + expect(response.status).toBe(404); + expect(response.body.message).toBe('Product not found'); + }, 10000); +}); +describe('Cart Order and payment functionalities', () => { + it('should create a cart for a product', async () => { + const productId = product1Id; + const quantity = 8; + + const token = getAccessToken(BuyerID, sampleBuyer1.email); + + const response = await request(app) + .post('/cart') + .set('Authorization', `Bearer ${token}`) + .send({ productId, quantity }); + - expect(response.status).toBe(404); - expect(response.body.status).toBe('error'); - expect(response.body.message).toBe('Product not found'); - }, 10000); + expect(response.status).toBe(201); + expect(response.body.data.cart).toBeDefined(); + cardID = JSON.stringify(response.body.data.cart.id) }); - \ No newline at end of file + +} +) \ No newline at end of file diff --git a/src/__test__/isAllowed.test.ts b/src/__test__/isAllowed.test.ts index 2eb18a8..471a950 100644 --- a/src/__test__/isAllowed.test.ts +++ b/src/__test__/isAllowed.test.ts @@ -5,6 +5,7 @@ import { getConnection } from 'typeorm'; import { User } from '../entities/User'; import { responseError } from '../utils/response.utils'; import { v4 as uuid } from 'uuid'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; jest.mock('../utils/response.utils'); @@ -47,31 +48,23 @@ beforeAll(async () => { }); afterAll(async () => { - const connection = getConnection(); - const userRepository = connection.getRepository(User); - - - // Delete all records from the User - await userRepository.delete({}); - - // Close the connection to the test database - await connection.close(); + await cleanDatabase(); }); describe('Middleware - checkUserStatus', () => { - beforeEach(() => { - reqMock = {}; - resMock = { - status: jest.fn().mockReturnThis(), - json: jest.fn() - }; - nextMock = jest.fn(); - }); - - it('should return 401 if user is not authenticated', async () => { - await checkUserStatus(reqMock as Request, resMock as Response, nextMock); - expect(responseError).toHaveBeenCalledWith(resMock, 401, 'Authentication required'); - }); + beforeEach(() => { + reqMock = {}; + resMock = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + }; + nextMock = jest.fn(); + }); + + it('should return 401 if user is not authenticated', async () => { + await checkUserStatus(reqMock as Request, resMock as Response, nextMock); + expect(responseError).toHaveBeenCalledWith(resMock, 401, 'Authentication required'); + }); it('should return 401 if user is not found', async () => { reqMock = { user: { id: uuid() } }; diff --git a/src/__test__/logout.test.ts b/src/__test__/logout.test.ts index d777f7a..ac9eefa 100644 --- a/src/__test__/logout.test.ts +++ b/src/__test__/logout.test.ts @@ -2,22 +2,15 @@ import request from 'supertest'; import { app, server } from '../index'; import { createConnection, getConnection, getConnectionOptions, getRepository } from 'typeorm'; import { User } from '../entities/User'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; beforeAll(async () => { // Connect to the test database - const connectionOptions = await getConnectionOptions(); - await createConnection({ ...connectionOptions, name: 'testConnection' }); + await createConnection(); }); afterAll(async () => { - const connection = getConnection('testConnection'); - const userRepository = connection.getRepository(User); - - // Delete all records from the User - await userRepository.delete({}); - - // Close the connection to the test database - await connection.close(); + await cleanDatabase(); server.close(); }); diff --git a/src/__test__/oauth.test.ts b/src/__test__/oauth.test.ts index 7723cb4..877d63b 100644 --- a/src/__test__/oauth.test.ts +++ b/src/__test__/oauth.test.ts @@ -2,36 +2,23 @@ import request from 'supertest'; import { app, server } from '../index'; import { createConnection, getConnection, getConnectionOptions, getRepository } from 'typeorm'; import { User } from '../entities/User'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; beforeAll(async () => { - // Connect to the test database - const connectionOptions = await getConnectionOptions(); - - await createConnection({ ...connectionOptions, name: 'testConnection' }); + await createConnection(); }); afterAll(async () => { - const connection = getConnection('testConnection'); - const userRepository = connection.getRepository(User); - - // Delete all records from the User - await userRepository.delete({}); - - // Close the connection to the test database - await connection.close(); - + await cleanDatabase(); server.close(); }); -describe('authentication routes test',() => { - it('should redirect to the google authentication page',async() => { - const response = await request(app) - .get('/user/google-auth'); - expect(response.statusCode).toBe(302) - }) - it('should redirect after google authentication', async() => { - const response = await request(app) - .get('/user/auth/google/callback'); - expect(response.statusCode).toBe(302) - }) +describe('authentication routes test', () => { + it('should redirect to the google authentication page', async () => { + const response = await request(app).get('/user/google-auth'); + expect(response.statusCode).toBe(302); + }); + it('should redirect after google authentication', async () => { + const response = await request(app).get('/user/auth/google/callback'); + expect(response.statusCode).toBe(302); + }); }); - diff --git a/src/__test__/orderManagement.test.ts b/src/__test__/orderManagement.test.ts new file mode 100644 index 0000000..846b9d8 --- /dev/null +++ b/src/__test__/orderManagement.test.ts @@ -0,0 +1,390 @@ +import request from 'supertest'; +import jwt from 'jsonwebtoken'; +import { app, server } from '../index'; +import { getConnection } from 'typeorm'; +import { dbConnection } from '../startups/dbConnection'; +import { User, UserInterface } from '../entities/User'; +import { v4 as uuid } from 'uuid'; +import { Product } from '../entities/Product'; +import { Category } from '../entities/Category'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; +import { Order } from '../entities/Order'; +import { OrderItem } from '../entities/OrderItem'; +import { VendorOrders } from '../entities/vendorOrders'; +import { VendorOrderItem } from '../entities/VendorOrderItem'; + +const adminId = uuid(); +const vendorId = uuid(); +const vendor2Id = uuid(); +const buyerId = uuid(); + +const productId = uuid(); +const product2Id = uuid(); + +const orderId = uuid(); +const orderItemId = uuid(); +const order2Id = uuid(); +const order2ItemId = uuid(); + +const vendorOrderId = uuid(); +const vendorOrderItemId = uuid(); +const vendorOrder2Id = uuid(); +const vendorOrder2ItemId = uuid(); +const catId = uuid(); + +console.log(adminId, vendorId, buyerId); + +const jwtSecretKey = process.env.JWT_SECRET || ''; + +const getAccessToken = (id: string, email: string) => { + return jwt.sign( + { + id: id, + email: email, + }, + jwtSecretKey + ); +}; + +const sampleAdmin: UserInterface = { + id: adminId, + firstName: 'admin', + lastName: 'user', + email: 'admin@example.com', + password: 'password', + userType: 'Admin', + gender: 'Male', + phoneNumber: '126380997', + photoUrl: 'https://example.com/photo.jpg', + verified: true, + role: 'ADMIN', +}; +const sampleVendor: UserInterface = { + id: vendorId, + firstName: 'vendor', + lastName: 'user', + email: 'vendor@example.com', + password: 'password', + userType: 'Vendor', + gender: 'Male', + phoneNumber: '126380996347', + photoUrl: 'https://example.com/photo.jpg', + verified: true, + role: 'VENDOR', +}; +const sampleVendor2: UserInterface = { + id: vendor2Id, + firstName: 'vendor', + lastName: 'user', + email: 'vendor2@example.com', + password: 'password', + userType: 'Vendor', + gender: 'Male', + phoneNumber: '18090296347', + photoUrl: 'https://example.com/photo.jpg', + verified: true, + role: 'VENDOR', +}; +const sampleBuyer: UserInterface = { + id: buyerId, + firstName: 'buyer', + lastName: 'user', + email: 'buyer@example.com', + password: 'password', + userType: 'Buyer', + gender: 'Male', + phoneNumber: '6380996347', + photoUrl: 'https://example.com/photo.jpg', + verified: true, + role: 'BUYER', +}; + +const sampleCat = { + id: catId, + name: 'accessories', +}; + +const sampleProduct = { + id: productId, + name: 'test product', + description: 'amazing product', + images: ['photo1.jpg', 'photo2.jpg', 'photo3.jpg'], + newPrice: 200, + quantity: 10, + vendor: sampleVendor, + categories: [sampleCat], +}; +const sampleProduct2 = { + id: product2Id, + name: 'test product2', + description: 'amazing products', + images: ['photo1.jpg', 'photo2.jpg', 'photo3.jpg'], + newPrice: 200, + quantity: 10, + vendor: sampleVendor, + categories: [sampleCat], +}; + +const sampleOrder = { + id: orderId, + totalPrice: 400, + quantity: 2, + orderDate: new Date(), + buyer: sampleBuyer, + orderStatus: 'received', + address: 'Rwanda, Kigali, KK20st', +}; +const sampleOrder2 = { + id: order2Id, + totalPrice: 400, + quantity: 2, + orderDate: new Date(), + buyer: sampleBuyer, + orderStatus: 'order placed', + address: 'Rwanda, Kigali, KK20st', +}; + +const sampleOrderItem = { + id: orderItemId, + price: 200, + quantity: 2, + order: sampleOrder, + product: sampleProduct, +}; + +const sampleVendorOrder = { + id: vendorOrderId, + totalPrice: 400, + quantity: 2, + vendor: sampleVendor, + order: sampleOrder, + buyer: sampleBuyer, + orderStatus: 'pending', +}; + +const sampleVendorOrderItem = { + 'id': vendorOrderItemId, + 'price/unit': 200, + 'quantity': 2, + 'order': sampleVendorOrder, + 'product': sampleProduct, +}; + +beforeAll(async () => { + const connection = await dbConnection(); + + const categoryRepository = connection?.getRepository(Category); + await categoryRepository?.save({ ...sampleCat }); + + const userRepository = connection?.getRepository(User); + await userRepository?.save([sampleAdmin, sampleVendor, sampleVendor2, sampleBuyer]); + + const productRepository = connection?.getRepository(Product); + await productRepository?.save({ ...sampleProduct }); + + // Order Management + const orderRepository = connection?.getRepository(Order); + await orderRepository?.save([sampleOrder, sampleOrder2]); + + const orderItemRepository = connection?.getRepository(OrderItem); + await orderItemRepository?.save({ ...sampleOrderItem }); + + const vendorOrderRepository = connection?.getRepository(VendorOrders); + await vendorOrderRepository?.save({ ...sampleVendorOrder }); + + const vendorOrderItemRepository = connection?.getRepository(VendorOrderItem); + await vendorOrderItemRepository?.save({ ...sampleVendorOrderItem }); +}); + +afterAll(async () => { + await cleanDatabase(); + server.close(); +}); + +describe('Vendor Order Management', () => { + describe('Fetching vendor Order(s)', () => { + it('Should return all vendor orders', async () => { + const response = await request(app) + .get('/product/vendor/orders') + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`); + + expect(response.status).toBe(200); + expect(response.body.data.orders).toBeDefined(); + }); + + it("Should return empty array if vendor don't have any order for buyer", async () => { + const response = await request(app) + .get('/product/vendor/orders') + .set('Authorization', `Bearer ${getAccessToken(vendor2Id, sampleVendor2.email)}`); + + expect(response.status).toBe(200); + expect(response.body.data.orders).toEqual([]); + }); + + it('Should return single vendor order', async () => { + const response = await request(app) + .get(`/product/vendor/orders/${vendorOrderId}`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`); + + expect(response.status).toBe(200); + expect(response.body.data.order).toBeDefined(); + }); + + it('return 404, for non existing vendor order', async () => { + const response = await request(app) + .get(`/product/vendor/orders/${uuid()}`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`); + + expect(response.status).toBe(404); + expect(response.body.message).toBe('Order Not Found.'); + }); + + it('return 400, for invalid vendor order id ', async () => { + const response = await request(app) + .get(`/product/vendor/orders/32df3`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`); + + expect(response.status).toBe(400); + expect(response.body.message).toBe(`invalid input syntax for type uuid: "32df3"`); + }); + }); + describe('Updating vendor order', () => { + it('should update the order', async () => { + const response = await request(app) + .put(`/product/vendor/orders/${vendorOrderId}`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`) + .send({ + orderStatus: 'delivered', + }); + + expect(response.statusCode).toBe(200); + }); + it('should not update if orderStatus in not among defined ones', async () => { + const response = await request(app) + .put(`/product/vendor/orders/${vendorOrderId}`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`) + .send({ + orderStatus: 'fakeOrderStatus', + }); + + expect(response.statusCode).toBe(400); + expect(response.body.message).toBe('Please provide one of defined statuses.'); + }); + it('should not update, return 404 for non existing vendor order', async () => { + const response = await request(app) + .put(`/product/vendor/orders/${uuid()}`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`) + .send({ + orderStatus: 'is-accepted', + }); + + expect(response.statusCode).toBe(404); + expect(response.body.message).toBe('Order Not Found.'); + }); + it('should not update, if the order has already been cancelled or completed', async () => { + const response = await request(app) + .put(`/product/vendor/orders/${vendorOrderId}`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`) + .send({ + orderStatus: 'is-accepted', + }); + + expect(response.statusCode).toBe(409); + }); + it('return 400, for invalid vendor order id ', async () => { + const response = await request(app) + .put(`/product/vendor/orders/32df3`) + .set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`) + .send({ + orderStatus: 'is-accepted', + }); + + expect(response.status).toBe(400); + expect(response.body.message).toBe(`invalid input syntax for type uuid: "32df3"`); + }); + }); +}); + +describe('Admin Order Management', () => { + describe('Fetching buyer and vendor Order(s)', () => { + it("Should return all orders with it's buyer and related vendors", async () => { + const response = await request(app) + .get('/product/admin/orders') + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.status).toBe(200); + expect(response.body.data.orders).toBeDefined(); + }); + + it('Should return single order details', async () => { + const response = await request(app) + .get(`/product/admin/orders/${orderId}`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.status).toBe(200); + expect(response.body.data.order).toBeDefined(); + }); + + it('return 404, for non existing order', async () => { + const response = await request(app) + .get(`/product/admin/orders/${uuid()}`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.status).toBe(404); + expect(response.body.message).toBe('Order Not Found.'); + }); + + it('return 400, for invalid order id ', async () => { + const response = await request(app) + .get(`/product/admin/orders/32df3`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.status).toBe(400); + expect(response.body.message).toBe(`invalid input syntax for type uuid: "32df3"`); + }); + }); + describe('Updating buyer and vendor order', () => { + it('should not update, return 404 for non existing order', async () => { + const response = await request(app) + .put(`/product/admin/orders/${uuid()}`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.statusCode).toBe(404); + expect(response.body.message).toBe('Order Not Found.'); + }); + it('should update the order', async () => { + const response = await request(app) + .put(`/product/admin/orders/${orderId}`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.statusCode).toBe(200); + expect(response.body.data.order).toBeDefined(); + }); + it('should not update if it has already been completed(closed)', async () => { + const response = await request(app) + .put(`/product/admin/orders/${orderId}`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.statusCode).toBe(409); + expect(response.body.message).toBe('The order has already been completed.'); + }); + + it('should not update, if the order has not been marked as received by buyer', async () => { + const response = await request(app) + .put(`/product/admin/orders/${order2Id}`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.statusCode).toBe(409); + expect(response.body.message).toBe('Order closure failed: The buyer has not received the item yet.'); + }); + + it('return 400, for invalid order id ', async () => { + const response = await request(app) + .put(`/product/admin/orders/32df3`) + .set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`); + + expect(response.status).toBe(400); + expect(response.body.message).toBe(`invalid input syntax for type uuid: "32df3"`); + }); + }); +}); diff --git a/src/__test__/productStatus.test.ts b/src/__test__/productStatus.test.ts index a770020..8e8b42a 100644 --- a/src/__test__/productStatus.test.ts +++ b/src/__test__/productStatus.test.ts @@ -7,6 +7,7 @@ import { User } from '../entities/User'; import { v4 as uuid } from 'uuid'; import { Product } from '../entities/Product'; import { Category } from '../entities/Category'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; const vendor1Id = uuid(); const vendor2Id = uuid(); @@ -143,17 +144,8 @@ beforeAll(async () => { }); afterAll(async () => { - const connection = getConnection(); - const userRepository = connection.getRepository(User); - const categoryRepository = connection.getRepository(Category); + await cleanDatabase(); - const productRepository = await connection.getRepository(Product).delete({}); - if (productRepository) { - await userRepository.delete({}); - await categoryRepository.delete({}); - } - - await connection.close(); server.close(); }); @@ -230,22 +222,16 @@ describe('Vendor product availability status management tests', () => { }); }); - describe('search product by name availability tests', () => { it('Should search product by name', async () => { - const response = await request(app) - .get(`/product/search?name=testingmkknkkjiproduct4`) + const response = await request(app).get(`/product/search?name=testingmkknkkjiproduct4`); expect(response.body.data).toBeDefined; }, 10000); it('should return empty array if there is product is not found in the database', async () => { - const response = await request(app) - .put(`/product/search?name=home`) - + const response = await request(app).put(`/product/search?name=home`); expect(response.statusCode).toBe(401); expect(response.body.data).toBeUndefined; }); - - }); - +}); diff --git a/src/__test__/roleCheck.test.ts b/src/__test__/roleCheck.test.ts index 58efb9c..32df044 100644 --- a/src/__test__/roleCheck.test.ts +++ b/src/__test__/roleCheck.test.ts @@ -5,6 +5,7 @@ import { responseError } from '../utils/response.utils'; import { dbConnection } from '../startups/dbConnection'; import { v4 as uuid } from 'uuid'; import { getConnection } from 'typeorm'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; let reqMock: Partial; let resMock: Partial; @@ -34,14 +35,7 @@ beforeAll(async () => { }); afterAll(async () => { - const connection = getConnection(); - const userRepository = connection.getRepository(User); - - // Delete all records from the User - await userRepository.delete({}); - - // Close the connection to the test database - await connection.close(); + await cleanDatabase(); }); describe('hasRole MiddleWare Test', () => { diff --git a/src/__test__/route.test.ts b/src/__test__/route.test.ts index 933c431..ac704b5 100644 --- a/src/__test__/route.test.ts +++ b/src/__test__/route.test.ts @@ -3,24 +3,15 @@ import { app, server } from '../index'; import { createConnection, getConnection, getConnectionOptions, getRepository } from 'typeorm'; import { User } from '../entities/User'; import { response } from 'express'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; beforeAll(async () => { - // Connect to the test database - const connectionOptions = await getConnectionOptions(); - - await createConnection({ ...connectionOptions, name: 'testConnection' }); + await createConnection(); }); jest.setTimeout(20000); afterAll(async () => { - const connection = getConnection('testConnection'); - const userRepository = connection.getRepository(User); - - // Delete all records from the User - await userRepository.delete({}); - - // Close the connection to the test database - await connection.close(); + await cleanDatabase(); server.close(); }); diff --git a/src/__test__/test-assets/DatabaseCleanup.ts b/src/__test__/test-assets/DatabaseCleanup.ts new file mode 100644 index 0000000..3674dfb --- /dev/null +++ b/src/__test__/test-assets/DatabaseCleanup.ts @@ -0,0 +1,50 @@ +import { Transaction } from '../../entities/transaction'; +import { Cart } from '../../entities/Cart'; +import { CartItem } from '../../entities/CartItem'; +import { Order } from '../../entities/Order'; +import { OrderItem } from '../../entities/OrderItem'; +import { wishList } from '../../entities/wishList'; +import { getConnection } from 'typeorm'; +import { Product } from '../../entities/Product'; +import { Category } from '../../entities/Category'; +import { Coupon } from '../../entities/coupon'; +import { User } from '../../entities/User'; +import { server } from '../..'; +import { VendorOrderItem } from '../../entities/VendorOrderItem'; +import { VendorOrders } from '../../entities/vendorOrders'; + +export const cleanDatabase = async () => { + const connection = getConnection(); + + // Delete from child tables first + await connection.getRepository(Transaction).delete({}); + await connection.getRepository(Coupon).delete({}); + await connection.getRepository(VendorOrderItem).delete({}); + await connection.getRepository(VendorOrders).delete({}); + await connection.getRepository(OrderItem).delete({}); + await connection.getRepository(Order).delete({}); + await connection.getRepository(CartItem).delete({}); + await connection.getRepository(Cart).delete({}); + await connection.getRepository(wishList).delete({}); + + // Many-to-Many relations + // Clear junction table entries before deleting products and categories + await connection.createQueryRunner().query('DELETE FROM product_categories_category'); + + await connection.getRepository(Product).delete({}); + await connection.getRepository(Category).delete({}); + + // Coupons (if related to Orders or Users) + + // Finally, delete from parent table + await connection.getRepository(User).delete({}); + + await connection.close(); +}; + +// // Execute the clean-up function +// cleanDatabase().then(() => { +// console.log('Database cleaned'); +// }).catch(error => { +// console.error('Error cleaning database:', error); +// }); diff --git a/src/__test__/userServices.test.ts b/src/__test__/userServices.test.ts index 5249a9a..29a2e7c 100644 --- a/src/__test__/userServices.test.ts +++ b/src/__test__/userServices.test.ts @@ -1,25 +1,15 @@ import request from 'supertest'; import { app, server } from '../index'; -import { createConnection, getConnection, getConnectionOptions, getRepository } from 'typeorm'; +import { createConnection, getRepository } from 'typeorm'; import { User } from '../entities/User'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; beforeAll(async () => { - // Connect to the test database - const connectionOptions = await getConnectionOptions(); - - await createConnection({ ...connectionOptions, name: 'testConnection' }); + await createConnection(); }); afterAll(async () => { - const connection = getConnection('testConnection'); - const userRepository = connection.getRepository(User); - - // Delete all records from the User - await userRepository.delete({}); - - // Close the connection to the test database - await connection.close(); - + await cleanDatabase(); server.close(); }); diff --git a/src/__test__/userStatus.test.ts b/src/__test__/userStatus.test.ts index 8d42488..69e892a 100644 --- a/src/__test__/userStatus.test.ts +++ b/src/__test__/userStatus.test.ts @@ -6,6 +6,7 @@ import { getConnection } from 'typeorm'; import { dbConnection } from '../startups/dbConnection'; import { User } from '../entities/User'; import { v4 as uuid } from 'uuid'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; const adminUserId = uuid(); @@ -35,14 +36,8 @@ beforeAll(async () => { }); afterAll(async () => { - const connection = getConnection(); - const userRepository = connection.getRepository(User); + await cleanDatabase(); - // Delete all records from the User - await userRepository.delete({}); - - // Close the connection to the test database - await connection.close(); server.close(); }); diff --git a/src/__test__/vendorProduct.test.ts b/src/__test__/vendorProduct.test.ts index dfa96c4..d8fc0a5 100644 --- a/src/__test__/vendorProduct.test.ts +++ b/src/__test__/vendorProduct.test.ts @@ -7,6 +7,7 @@ import { User, UserInterface } from '../entities/User'; import { v4 as uuid } from 'uuid'; import { Product } from '../entities/Product'; import { Category } from '../entities/Category'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; const vendor1Id = uuid(); const vendor2Id = uuid(); @@ -109,18 +110,8 @@ beforeAll(async () => { }); afterAll(async () => { - const connection = getConnection(); - const userRepository = connection.getRepository(User); - const categoryRepository = connection.getRepository(Category); - - const productRepository = await connection.getRepository(Product).delete({}); - if (productRepository) { - await userRepository.delete({}); - await categoryRepository.delete({}); - } - - // Close the connection to the test database - await connection.close(); + await cleanDatabase(); + server.close(); }); @@ -456,29 +447,27 @@ describe('Vendor product management tests', () => { describe('List all products service', () => { it('should return all products for a given category', async () => { - const response = await request(app) - .get('/product/all') + const response = await request(app).get('/product/all'); expect(response.status).toBe(200); expect(response.body.data.products).toBeDefined(); }); - + it('should return no products for a non-existent category', async () => { const response = await request(app) .get('/product/all') - .query({ page: 1, limit: 10, category: 'nonexistentcategory' }) - + .query({ page: 1, limit: 10, category: 'nonexistentcategory' }); + expect(response.status).toBe(200); expect(response.body.data.products).toBeUndefined(); }); - + it('should return an error for invalid input syntax', async () => { const response = await request(app) .get('/product/all') - .query({ page: 'invalid', limit: 'limit', category: 'technology' }) - + .query({ page: 'invalid', limit: 'limit', category: 'technology' }); + expect(response.status).toBe(400); }); }); - }); diff --git a/src/__test__/wishList.test.ts b/src/__test__/wishList.test.ts index 2b2f120..6658853 100644 --- a/src/__test__/wishList.test.ts +++ b/src/__test__/wishList.test.ts @@ -8,6 +8,7 @@ import { Product } from '../entities/Product'; import { Category } from '../entities/Category'; import { wishList } from '../entities/wishList'; import { User, UserInterface } from '../entities/User'; +import { cleanDatabase } from './test-assets/DatabaseCleanup'; const buyer1Id = uuid(); const buyer2Id = uuid(); @@ -17,222 +18,187 @@ const catId = uuid(); const vendor2Id = uuid(); const sampleBuyer1: UserInterface = { - id: buyer1Id, - firstName: 'buyer1', - lastName: 'user', - email: 'buyer1@example.com', - password: 'password', - userType: 'Buyer', - gender: 'Male', - phoneNumber: '126380996347', - photoUrl: 'https://example.com/photo.jpg', - role: 'BUYER', - }; - const sampleBuyer2: UserInterface = { - id: buyer2Id, - firstName: 'buyer2', - lastName: 'use', - email: 'buyer2@example.com', - password: 'passwo', - userType: 'Buyer', - gender: 'Male', - phoneNumber: '1638099347', - photoUrl: 'https://example.com/photo.jpg', - role: 'BUYER', - }; - const sampleVendor1: UserInterface = { - id: vendor2Id, - firstName: 'vendor1', - lastName: 'user', - email: 'vendor11@example.com', - password: 'password', - userType: 'Vendor', - gender: 'Male', - phoneNumber: '12638090347', - photoUrl: 'https://example.com/photo.jpg', - role: 'VENDOR', - }; + id: buyer1Id, + firstName: 'buyer1', + lastName: 'user', + email: 'buyer1@example.com', + password: 'password', + userType: 'Buyer', + gender: 'Male', + phoneNumber: '126380996347', + photoUrl: 'https://example.com/photo.jpg', + role: 'BUYER', +}; +const sampleBuyer2: UserInterface = { + id: buyer2Id, + firstName: 'buyer2', + lastName: 'use', + email: 'buyer2@example.com', + password: 'passwo', + userType: 'Buyer', + gender: 'Male', + phoneNumber: '1638099347', + photoUrl: 'https://example.com/photo.jpg', + role: 'BUYER', +}; +const sampleVendor1: UserInterface = { + id: vendor2Id, + firstName: 'vendor1', + lastName: 'user', + email: 'vendor11@example.com', + password: 'password', + userType: 'Vendor', + gender: 'Male', + phoneNumber: '12638090347', + photoUrl: 'https://example.com/photo.jpg', + role: 'VENDOR', +}; let productInWishList: number; beforeAll(async () => { - const connection = await dbConnection(); - const userRepository = connection?.getRepository(User); - await userRepository?.save({ ...sampleBuyer1 }); - await userRepository?.save({ ...sampleBuyer2 }); - await userRepository?.save({...sampleVendor1}) - }); + const connection = await dbConnection(); + const userRepository = connection?.getRepository(User); + await userRepository?.save({ ...sampleBuyer1 }); + await userRepository?.save({ ...sampleBuyer2 }); + await userRepository?.save({ ...sampleVendor1 }); +}); + +afterAll(async () => { + await cleanDatabase(); + server.close(); +}); +const data1 = { + id: buyer1Id, + email: sampleBuyer1.email, +}; +const data2 = { + id: buyer2Id, + email: sampleBuyer2.email, +}; +const vendorData = { + id: vendor2Id, + email: sampleVendor1.email, +}; - afterAll(async () => { - const connection = getConnection(); - const userRepository = connection.getRepository(User); - const categoryRepository = connection.getRepository(Category); - const wishListRepository = connection.getRepository(wishList) - - // Delete all records - const productRepository = await connection.getRepository(Product).delete({}); - if (productRepository) { - await userRepository.delete({}); - await categoryRepository.delete({}); - } - await userRepository.delete({}); - await wishListRepository.delete({}) - - // Close the connection to the test database - await connection.close(); - server.close(); +const jwtSecretKey = process.env.JWT_SECRET || ''; +describe('Wish list management tests', () => { + describe('Add product to wish list', () => { + it('should return 404 when product is not found', async () => { + const token = jwt.sign(data1, jwtSecretKey); + const response = await request(app).post(`/wish-list/add/${uuid()}`).set('Authorization', `Bearer ${token}`); + expect(response.status).toBe(404); + expect(response.body).toEqual({ message: 'Product not found' }); + }); + + it('should add a new product to wish list', async () => { + const vendorToken = jwt.sign(vendorData, jwtSecretKey); + const prod1Response = await request(app) + .post('/product') + .field('name', 'test product12') + .field('description', 'amazing product3') + .field('newPrice', 2000) + .field('quantity', 10) + .field('categories', 'technology') + .field('categories', 'sample') + .attach('images', `${__dirname}/test-assets/photo1.png`) + .attach('images', `${__dirname}/test-assets/photo2.webp`) + .set('Authorization', `Bearer ${vendorToken}`); + + product1Id = prod1Response.body.data.product.id; + + const prod2Response = await request(app) + .post('/product') + .field('name', 'more product2') + .field('description', 'food product3') + .field('newPrice', 2000) + .field('quantity', 10) + .field('categories', 'technology') + .field('categories', 'sample') + .attach('images', `${__dirname}/test-assets/photo1.png`) + .attach('images', `${__dirname}/test-assets/photo2.webp`) + .set('Authorization', `Bearer ${vendorToken}`); + + product2Id = prod2Response.body.data.product.id; + + const token = jwt.sign(data1, jwtSecretKey); + const response1 = await request(app).post(`/wish-list/add/${product1Id}`).set('Authorization', `Bearer ${token}`); + expect(response1.status).toBe(201); + expect(response1.body.data.message).toBe('Product Added to wish list'); + productInWishList = response1.body.data.wishlistAdded.id; + + await request(app).post(`/wish-list/add/${product2Id}`).set('Authorization', `Bearer ${token}`); + }); + + it('should tell if there is the product is already in the wish list', async () => { + const token = jwt.sign(data1, jwtSecretKey); + const response = await request(app).post(`/wish-list/add/${product1Id}`).set('Authorization', `Bearer ${token}`); + expect(response.status).toBe(401); + expect(response.body.data.message).toBe('Product Already in the wish list'); + }); + it('should return 500 when the ID is not valid', async () => { + const token = jwt.sign(data1, jwtSecretKey); + const response = await request(app) + .post(`/wish-list/add/kjwxq-wbjk2-2bwqs-21`) + .set('Authorization', `Bearer ${token}`); + expect(response.status).toBe(500); + }); }); - const data1 = { - id: buyer1Id, - email: sampleBuyer1.email - }; - const data2 = { - id: buyer2Id, - email: sampleBuyer2.email - } - const vendorData = { - id: vendor2Id, - email: sampleVendor1.email - } + describe('Get products in wishList', () => { + it('Returns 404 when buyer has no product in wish list', async () => { + const token = jwt.sign(data2, jwtSecretKey); + const response = await request(app).get('/wish-list').set('Authorization', `Bearer ${token}`); + expect(response.status).toBe(404); + expect(response.body.message).toBe('No products in wish list'); + }); + + it('Returns products in the wish list for a buyer ', async () => { + const token = jwt.sign(data1, jwtSecretKey); + const response = await request(app).get('/wish-list').set('Authorization', `Bearer ${token}`); + expect(response.status).toBe(200); + expect(response.body.message).toBe('Products retrieved'); + }); + }); -const jwtSecretKey = process.env.JWT_SECRET || ''; -describe('Wish list management tests', () => { + describe('Remove a product from wish lsit', () => { + it('should return 404 when product is not found in wish list', async () => { + const token = jwt.sign(data1, jwtSecretKey); + const response = await request(app).delete(`/wish-list/delete/${28}`).set('Authorization', `Bearer ${token}`); + expect(response.status).toBe(404); + expect(response.body.message).toBe('Product not found in wish list'); + }); + + it('should delete a product from wish list', async () => { + const token = jwt.sign(data1, jwtSecretKey); + const response = await request(app) + .delete(`/wish-list/delete/${productInWishList}`) + .set('Authorization', `Bearer ${token}`); + expect(response.status).toBe(200); + expect(response.body.message).toBe('Product removed from wish list'); + }); + it('should return 500 when the ID is not valid', async () => { + const token = jwt.sign(data1, jwtSecretKey); + const response = await request(app) + .delete(`/wish-list/delete/kjwxq-wbjk2-2bwqs-21`) + .set('Authorization', `Bearer ${token}`); + expect(response.status).toBe(500); + }); + }); - describe('Add product to wish list', () => { - it('should return 404 when product is not found', async () => { - const token = jwt.sign(data1, jwtSecretKey); - const response = await request(app) - .post( `/wish-list/add/${uuid()}`) - .set('Authorization', `Bearer ${token}`); - expect(response.status).toBe(404); - expect(response.body).toEqual({ message: 'Product not found' }); - }) - - it('should add a new product to wish list', async () =>{ - const vendorToken = jwt.sign(vendorData, jwtSecretKey); - const prod1Response = await request(app) - .post('/product') - .field('name', 'test product12') - .field('description', 'amazing product3') - .field('newPrice', 2000) - .field('quantity', 10) - .field('categories', 'technology') - .field('categories', 'sample') - .attach('images', `${__dirname}/test-assets/photo1.png`) - .attach('images', `${__dirname}/test-assets/photo2.webp`) - .set('Authorization', `Bearer ${vendorToken}`); - - product1Id = prod1Response.body.data.product.id; - - const prod2Response = await request(app) - .post('/product') - .field('name', 'more product2') - .field('description', 'food product3') - .field('newPrice', 2000) - .field('quantity', 10) - .field('categories', 'technology') - .field('categories', 'sample') - .attach('images', `${__dirname}/test-assets/photo1.png`) - .attach('images', `${__dirname}/test-assets/photo2.webp`) - .set('Authorization', `Bearer ${vendorToken}`); - - product2Id = prod2Response.body.data.product.id; - - const token = jwt.sign(data1, jwtSecretKey); - const response1 = await request(app) - .post( `/wish-list/add/${product1Id}`) - .set('Authorization', `Bearer ${token}`); - expect(response1.status).toBe(201); - expect(response1.body.data.message).toBe('Product Added to wish list'); - productInWishList = response1.body.data.wishlistAdded.id; - - await request(app) - .post( `/wish-list/add/${product2Id}`) - .set('Authorization', `Bearer ${token}`); - }) - - it('should tell if there is the product is already in the wish list', async () => { - const token = jwt.sign(data1, jwtSecretKey); - const response = await request(app) - .post( `/wish-list/add/${product1Id}`) - .set('Authorization', `Bearer ${token}`); - expect(response.status).toBe(401); - expect(response.body.data.message).toBe('Product Already in the wish list'); - }) - it('should return 500 when the ID is not valid', async() => { - const token = jwt.sign(data1, jwtSecretKey); - const response = await request(app) - .post( `/wish-list/add/kjwxq-wbjk2-2bwqs-21`) - .set('Authorization', `Bearer ${token}`); - expect(response.status).toBe(500); - }) - }) - - describe('Get products in wishList', () => { - it('Returns 404 when buyer has no product in wish list', async () => { - const token = jwt.sign(data2, jwtSecretKey); - const response = await request(app) - .get('/wish-list') - .set('Authorization', `Bearer ${token}`); - expect(response.status).toBe(404); - expect(response.body.message).toBe('No products in wish list'); - }) - - it('Returns products in the wish list for a buyer ', async () => { - const token = jwt.sign(data1, jwtSecretKey); - const response = await request(app) - .get('/wish-list') - .set('Authorization', `Bearer ${token}`); - expect(response.status).toBe(200); - expect(response.body.message).toBe('Products retrieved'); - }) - }) - - describe('Remove a product from wish lsit', () => { - it('should return 404 when product is not found in wish list', async () => { - const token = jwt.sign(data1, jwtSecretKey); - const response = await request(app) - .delete( `/wish-list/delete/${28}`) - .set('Authorization', `Bearer ${token}`); - expect(response.status).toBe(404); - expect(response.body.message).toBe('Product not found in wish list'); - }) - - it('should delete a product from wish list', async () => { - const token = jwt.sign(data1, jwtSecretKey); - const response = await request(app) - .delete( `/wish-list/delete/${productInWishList}`) - .set('Authorization', `Bearer ${token}`); - expect(response.status).toBe(200); - expect(response.body.message).toBe('Product removed from wish list'); - }) - it('should return 500 when the ID is not valid', async() => { - const token = jwt.sign(data1, jwtSecretKey); - const response = await request(app) - .delete( `/wish-list/delete/kjwxq-wbjk2-2bwqs-21`) - .set('Authorization', `Bearer ${token}`); - expect(response.status).toBe(500); - }) - }) - - describe('Clear all products in wish for a user', () => { - it('Returns 404 when buyer has no product in wish list', async () => { - const token = jwt.sign(data2, jwtSecretKey); - const response = await request(app) - .delete( '/wish-list/clearAll') - .set('Authorization', `Bearer ${token}`); - expect(response.status).toBe(404); - expect(response.body.message).toBe('No products in wish list'); - }) - - it('should delete all products for a nuyer in wish list', async () => { - const token = jwt.sign(data1, jwtSecretKey); - const response = await request(app) - .delete( '/wish-list/clearAll') - .set('Authorization', `Bearer ${token}`); - expect(response.status).toBe(200); - expect(response.body.message).toBe('All products removed successfully'); - }) - }) -}) \ No newline at end of file + describe('Clear all products in wish for a user', () => { + it('Returns 404 when buyer has no product in wish list', async () => { + const token = jwt.sign(data2, jwtSecretKey); + const response = await request(app).delete('/wish-list/clearAll').set('Authorization', `Bearer ${token}`); + expect(response.status).toBe(404); + expect(response.body.message).toBe('No products in wish list'); + }); + + it('should delete all products for a nuyer in wish list', async () => { + const token = jwt.sign(data1, jwtSecretKey); + const response = await request(app).delete('/wish-list/clearAll').set('Authorization', `Bearer ${token}`); + expect(response.status).toBe(200); + expect(response.body.message).toBe('All products removed successfully'); + }); + }); +}); diff --git a/src/controllers/adminOrdercontroller.ts b/src/controllers/adminOrdercontroller.ts new file mode 100644 index 0000000..388220d --- /dev/null +++ b/src/controllers/adminOrdercontroller.ts @@ -0,0 +1,18 @@ +import { Request, Response } from 'express'; +import { + getSingleBuyerVendorOrderService, + getBuyerVendorOrdersService, + updateBuyerVendorOrderService, +} from '../services'; + +export const getBuyerVendorOrders = async (req: Request, res: Response) => { + await getBuyerVendorOrdersService(req, res); +}; + +export const getSingleBuyerVendorOrder = async (req: Request, res: Response) => { + await getSingleBuyerVendorOrderService(req, res); +}; + +export const updateBuyerVendorOrder = async (req: Request, res: Response) => { + await updateBuyerVendorOrderService(req, res); +}; diff --git a/src/controllers/couponController.ts b/src/controllers/couponController.ts new file mode 100644 index 0000000..e5a6804 --- /dev/null +++ b/src/controllers/couponController.ts @@ -0,0 +1,31 @@ +import { Request, Response } from 'express'; +import { createCouponService } from '../services/couponServices/createCouponService'; +import { updateCouponService } from '../services/couponServices/updateService'; +import { deleteCouponService } from '../services/couponServices/deleteCoupon'; +import { accessAllCouponService } from '../services/couponServices/accessAllCoupon'; +import { readCouponService } from '../services/couponServices/readCoupon'; +import { buyerApplyCouponService } from '../services/couponServices/buyerApplyCoupon'; + +export const createCoupon = async (req: Request, res: Response) => { + await createCouponService(req, res); +}; + +export const updateCoupon = async (req: Request, res: Response) => { + await updateCouponService(req, res); +}; + +export const deleteCoupon = async (req: Request, res: Response) => { + await deleteCouponService(req, res); +}; + +export const accessAllCoupon = async (req: Request, res: Response) => { + await accessAllCouponService(req, res); +}; + +export const readCoupon = async (req: Request, res: Response) => { + await readCouponService(req, res); +}; + +export const buyerApplyCoupon = async (req: Request, res: Response) => { + await buyerApplyCouponService(req, res); +}; diff --git a/src/controllers/index.ts b/src/controllers/index.ts index c7f3221..70dea3b 100644 --- a/src/controllers/index.ts +++ b/src/controllers/index.ts @@ -1,2 +1,5 @@ export * from './authController'; export * from './productController'; +export * from './orderController'; +export * from './vendorOrderController'; +export * from './adminOrdercontroller'; diff --git a/src/controllers/orderController.ts b/src/controllers/orderController.ts new file mode 100644 index 0000000..d4ac5fc --- /dev/null +++ b/src/controllers/orderController.ts @@ -0,0 +1,18 @@ +import { Request, Response } from 'express'; +import { createOrderService } from '../services/orderServices/createOrder'; +import { getOrdersService } from '../services/orderServices/getOrderService'; +import { updateOrderService } from '../services/orderServices/updateOrderService'; +import { getTransactionHistoryService } from '../services/orderServices/getOrderTransactionHistory'; + +export const createOrder = async (req: Request, res: Response) => { + await createOrderService(req, res); +}; +export const getOrders = async (req: Request, res: Response) => { + await getOrdersService(req, res); +}; +export const updateOrder = async (req: Request, res: Response) => { + await updateOrderService(req, res); +}; +export const getOrdersHistory = async (req: Request, res: Response) => { + await getTransactionHistoryService(req, res); +}; diff --git a/src/controllers/productController.ts b/src/controllers/productController.ts index 11caddd..05aa5a3 100644 --- a/src/controllers/productController.ts +++ b/src/controllers/productController.ts @@ -1,26 +1,18 @@ import { Request, Response } from 'express'; import { - createProductService, - updateProductService, - - removeProductImageService, - + removeProductImageService, readProductService, - readProductsService, - + readProductsService, deleteProductService, - getRecommendedProductsService, productStatusServices, viewSingleProduct, - searchProductService - -, - listAllProductsService} -from '../services'; - + searchProductService, + listAllProductsService, + confirmPayment, +} from '../services'; export const readProduct = async (req: Request, res: Response) => { await readProductService(req, res); @@ -50,10 +42,10 @@ export const getRecommendedProducts = async (req: Request, res: Response) => { await getRecommendedProductsService(req, res); }; - export const listAllProducts = async (req: Request, res: Response) => { await listAllProductsService(req, res); -};export const productStatus = async (req: Request, res: Response) => { +}; +export const productStatus = async (req: Request, res: Response) => { await productStatusServices(req, res); }; export const singleProduct = async (req: Request, res: Response) => { @@ -79,3 +71,6 @@ export const searchProduct = async (req: Request, res: Response) => { res.status(500).json({ error: 'Internal Server Error' }); } }; +export const Payment = async (req: Request, res: Response) => { + await confirmPayment(req, res); +}; diff --git a/src/controllers/vendorOrderController.ts b/src/controllers/vendorOrderController.ts new file mode 100644 index 0000000..955b01c --- /dev/null +++ b/src/controllers/vendorOrderController.ts @@ -0,0 +1,14 @@ +import { Request, Response } from 'express'; +import { getVendorOrdersService, getSingleVendorOrderService, updateVendorOrderService } from '../services'; + +export const getVendorOrders = async (req: Request, res: Response) => { + await getVendorOrdersService(req, res); +}; + +export const getSingleVendorOrder = async (req: Request, res: Response) => { + await getSingleVendorOrderService(req, res); +}; + +export const updateVendorOrder = async (req: Request, res: Response) => { + await updateVendorOrderService(req, res); +}; diff --git a/src/controllers/wishListController.ts b/src/controllers/wishListController.ts index e0cd1bd..23fa03f 100644 --- a/src/controllers/wishListController.ts +++ b/src/controllers/wishListController.ts @@ -1,23 +1,18 @@ import { Request, Response } from 'express'; -import{ - addProductService, - getProductsService, - removeProductService, - clearAllProductService -} from '../services' +import { addProductService, getProductsService, removeProductService, clearAllProductService } from '../services'; export const wishlistAddProduct = async (req: Request, res: Response) => { - await addProductService(req, res); - }; + await addProductService(req, res); +}; - export const wishlistRemoveProduct = async (req: Request, res:Response) => { - await removeProductService(req, res); - } +export const wishlistRemoveProduct = async (req: Request, res: Response) => { + await removeProductService(req, res); +}; - export const wishlistGetProducts = async (req: Request, res:Response) => { - await getProductsService(req, res); - } +export const wishlistGetProducts = async (req: Request, res: Response) => { + await getProductsService(req, res); +}; - export const wishlistClearAllProducts = async (req: Request, res:Response) => { - await clearAllProductService(req, res); - } \ No newline at end of file +export const wishlistClearAllProducts = async (req: Request, res: Response) => { + await clearAllProductService(req, res); +}; diff --git a/src/docs/adminOrderManagement.yml b/src/docs/adminOrderManagement.yml new file mode 100644 index 0000000..e8d6ed6 --- /dev/null +++ b/src/docs/adminOrderManagement.yml @@ -0,0 +1,80 @@ +/product/admin/orders: + get: + tags: + - Admin Order Manangement + summary: Fetches all buyer and vendor orders + description: Return all order including details for buyer and vendors of products in that order + security: + - bearerAuth: [] + responses: + '200': + description: Return all order + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '500': + description: Internal server error + +/product/admin/orders/{id}: + get: + tags: + - Admin Order Manangement + summary: Fetch details for single buyer and vendor order + description: + Fetch details for single order using buyer id, if successful return order details with it's corresponding vendor + security: + - bearerAuth: [] + parameters: + - in: path + name: id + schema: + type: string + required: true + description: The id of a buyer order + responses: + '200': + description: Order details retrieved successfully + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Order not found + '500': + description: Internal server error + put: + tags: + - Admin Order Manangement + summary: Updates order status for both buyer and vendor order + description: Updates orderStatus field of order, if successful returns updated order. + security: + - bearerAuth: [] + parameters: + - in: path + name: id + schema: + type: string + required: true + description: The id of a buyer order + consumes: + - application/json + responses: + '200': + description: Order was successfully updated, return updated order + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Order not found + '409': + description: Order can not be updated because (it has already been completed(close), delivered, cancelled) + '500': + description: Internal server error diff --git a/src/docs/couponDocs.yml b/src/docs/couponDocs.yml new file mode 100644 index 0000000..fefa829 --- /dev/null +++ b/src/docs/couponDocs.yml @@ -0,0 +1,217 @@ +/coupons/vendor/:id/access-coupons: + get: + tags: + - Vendor discount coupon management + summary: List all coupons + description: Return all coupons for the logged user + security: + - bearerAuth: [] + responses: + '200': + description: Return all coupons + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '500': + description: Internal server error + +/coupons/vendor/:id/checkout/:code: + get: + tags: + - Vendor discount coupon management + summary: Get a single coupon + description: Return a coupon based on the provided code + security: + - bearerAuth: [] + parameters: + - in: path + name: code + schema: + type: string + required: true + description: The code of the coupon + responses: + '200': + description: Return info for the coupon + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Coupon not found + '500': + description: Internal server error + +/coupons/coupons/vendor/:id: + post: + tags: + - Vendor discount coupon management + summary: Creates a new coupon + security: + - bearerAuth: [] + consumes: + - application/json + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + code: + type: string + discountType: + type: string + discountRate: + type: number + maxUsageLimit: + type: number + quantity: + type: number + product: + type: string + expirationDate: + type: string + format: date + required: + - code + - discountType + - maxUsageLimit + - product + responses: + '201': + description: Successfully added the coupon + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Coupon not found + '500': + description: Internal server error + +/coupons/coupons/vendor/:id/update-coupon/:code: + put: + tags: + - Vendor discount coupon management + summary: Update a coupon + security: + - bearerAuth: [] + parameters: + - in: path + name: code + schema: + type: string + required: true + description: The code of the coupon + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + code: + type: string + discountType: + type: string + discountRate: + type: number + maxUsageLimit: + type: number + quantity: + type: number + product: + type: string + expirationDate: + type: string + format: date + responses: + '200': + description: Successfully updated the coupon + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Coupon not found + '500': + description: Internal server error + +/coupons/vendor/:id/checkout/delete: + delete: + tags: + - Vendor discount coupon management + summary: Delete a coupon + security: + - bearerAuth: [] + parameters: + - in: path + name: id + schema: + type: string + required: true + description: The ID of the vendor + - in: query + name: code + schema: + type: string + required: true + description: The code of the coupon + responses: + '200': + description: Successfully deleted the coupon + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Coupon not found + '500': + description: Internal server error + +/coupons/apply: + post: + tags: + - Buyer Coupon Discount Management + summary: Give discount according to coupon code + description: Buyer gets discount on a product when all the checks pass + security: + - bearerAuth: [] + consumes: + - application/json + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + couponCode: + type: string + required: + - couponCode + responses: + '200': + description: Successfully Got Discount + '400': + description: Bad Request (Syntax error, No coupon code provide, Coupon is expired, Coupon Discount Ended,etc) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Coupon not found, No cart or product with that coupon is not in cart + '500': + description: Internal server error diff --git a/src/docs/orderDocs.yml b/src/docs/orderDocs.yml new file mode 100644 index 0000000..fcb620e --- /dev/null +++ b/src/docs/orderDocs.yml @@ -0,0 +1,108 @@ +paths: + /product/orders: + post: + tags: + - Order + summary: Make an order + description: Create a new order for the authenticated user + security: + - bearerAuth: [] + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + address: + type: object + properties: + country: + type: string + description: The country of the shipping address + city: + type: string + description: The city of the shipping address + street: + type: string + description: The street address + required: + - address + responses: + '201': + description: Order created successfully + '400': + description: Bad Request + '401': + description: Unauthorized + '500': + description: Internal Server Error + + /product/client/orders: + get: + tags: + - Order + summary: Get all orders + description: Retrieve all orders for the authenticated user + security: + - bearerAuth: [] + responses: + '200': + description: Orders retrieved successfully + '401': + description: Unauthorized + '500': + description: Internal Server Error + + /product/orders/history: + get: + tags: + - Order + summary: Get transaction history + description: Retrieve transaction history for the authenticated user + security: + - bearerAuth: [] + responses: + '200': + description: Transaction history retrieved successfully + '401': + description: Unauthorized + '500': + description: Internal Server Error + + /product/client/orders/:orderId: + put: + tags: + - Order + summary: Update order status + description: Update the status of a specific order for the authenticated user + security: + - bearerAuth: [] + parameters: + - in: path + name: orderId + schema: + type: string + required: true + description: The ID of the order + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + orderStatus: + type: string + description: The new status of the order + responses: + '200': + description: Order updated successfully + '400': + description: Bad Request + '401': + description: Unauthorized + '404': + description: Order not found + '500': + description: Internal Server Error diff --git a/src/docs/vendorOrderManagement.yml b/src/docs/vendorOrderManagement.yml new file mode 100644 index 0000000..5873717 --- /dev/null +++ b/src/docs/vendorOrderManagement.yml @@ -0,0 +1,93 @@ +/product/vendor/orders: + get: + tags: + - Vendor Order Manangement + summary: Fetches all vendor orders + description: Return all order for authenticated vendor + security: + - bearerAuth: [] + responses: + '200': + description: Return all order for vendor requested from buyer + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '500': + description: Internal server error + +/product/vendor/orders/{id}: + get: + tags: + - Vendor Order Manangement + summary: Fetch details for single vendor order + description: + Fetch details for single order for authenticated vendor, order that include only his/her product which a buyer has + requested in his order. + security: + - bearerAuth: [] + parameters: + - in: path + name: id + schema: + type: string + required: true + description: The id of a vendor order + responses: + '200': + description: Order details retrieved successfully + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Order not found + '500': + description: Internal server error + put: + tags: + - Vendor Order Manangement + summary: Updates order status for vendor order + description: + Updates orderStatus field of vendor order for authenticated vendor, it order that include only his/her product + which a buyer has request in his order. + security: + - bearerAuth: [] + parameters: + - in: path + name: id + schema: + type: string + required: true + description: The id of a vendor order + consumes: + - application/json + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + orderStatus: + type: string + example: "'is-accepted', 'in-transit', 'cancelled', 'delivered'" + responses: + '200': + description: Order was successfully updated, return updated order + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Order not found + '409': + description: Order can not be updated because (it has already been completed(close), delivered, cancelled) + '500': + description: Internal server error diff --git a/src/docs/vendorProduct.yml b/src/docs/vendorProduct.yml index 937b097..3830bf4 100644 --- a/src/docs/vendorProduct.yml +++ b/src/docs/vendorProduct.yml @@ -35,7 +35,7 @@ description: The id of product responses: '200': - description: Return info for the product + description: Return info for the product '400': description: Bad Request (syntax error, incorrect input format, etc..) '401': @@ -59,7 +59,7 @@ requestBody: required: true content: - application/json: + application/json: schema: type: object properties: @@ -75,10 +75,10 @@ type: file categories: oneOf: - - type: string - - type: array - items: - type: string + - type: string + - type: array + items: + type: string example: "'category' or ['category1', 'category2', ...]" expirationDate: type: string @@ -159,7 +159,7 @@ description: Product not found '500': description: Internal server error - + /product/images/{id}: delete: tags: diff --git a/src/docs/wishListDocs.yml b/src/docs/wishListDocs.yml new file mode 100644 index 0000000..7f705f7 --- /dev/null +++ b/src/docs/wishListDocs.yml @@ -0,0 +1,97 @@ +/wish-list: + get: + tags: + - Wish list + summary: Get all products in wishlist + description: Return all products in wish list for authenticated buyer + security: + - bearerAuth: [] + responses: + '200': + description: Return all products in wish list for a buyer + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '500': + description: Internal server error + +/wish-list/add/{id}: + post: + tags: + - Wish list + summary: Add product to wish list + description: Adds selected product (product id) to the wish list + security: + - bearerAuth: [] + parameters: + - in: path + name: id + schema: + type: string + required: true + description: Product id + responses: + '201': + description: Product Added to wish list + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Product not found in wish list + '500': + description: Internal server error + +/wish-list/delete/{id}: + delete: + tags: + - Wish list + summary: Remove product from wish list + description: Remove product from wish list for an authenticated buyer + security: + - bearerAuth: [] + parameters: + - in: path + name: id + schema: + type: string + required: true + description: Product id + responses: + '200': + description: Product removed from wish list + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '404': + description: Product not found in wish list + '500': + description: Internal server error + +/wish-list/clearAll: + delete: + tags: + - Wish list + summary: Clear entire wish list + description: Clears entire wish list for authenticated buyer + security: + - bearerAuth: [] + responses: + '200': + description: All products removed successfully + '400': + description: Bad Request (syntax error, incorrect input format, etc..) + '401': + description: Unauthorized + '403': + description: Forbidden (Unauthorized action) + '500': + description: Internal server error diff --git a/src/entities/Cart.ts b/src/entities/Cart.ts index 0ba44a6..fda1e15 100644 --- a/src/entities/Cart.ts +++ b/src/entities/Cart.ts @@ -36,7 +36,7 @@ export class Cart { @UpdateDateColumn() updatedAt!: Date; - updateTotal(): void { + updateTotal (): void { if (this.items) { let total: number = 0; for (let i = 0; i < this.items.length; i++) { diff --git a/src/entities/CartItem.ts b/src/entities/CartItem.ts index 107170c..d651adf 100644 --- a/src/entities/CartItem.ts +++ b/src/entities/CartItem.ts @@ -47,7 +47,7 @@ export class CartItem { @BeforeInsert() @BeforeUpdate() - updateTotal(): void { + updateTotal (): void { this.total = this.newPrice * this.quantity; } } diff --git a/src/entities/Order.ts b/src/entities/Order.ts index f52012e..47649a7 100644 --- a/src/entities/Order.ts +++ b/src/entities/Order.ts @@ -1,7 +1,16 @@ -import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, CreateDateColumn, UpdateDateColumn } from 'typeorm'; -import { IsNotEmpty, IsNumber, IsDate } from 'class-validator'; +import { + Entity, + PrimaryGeneratedColumn, + Column, + ManyToOne, + OneToMany, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm'; +import { IsNotEmpty, IsNumber, IsDate, IsIn } from 'class-validator'; import { User } from './User'; -import { Product } from './Product'; +import { OrderItem } from './OrderItem'; +import { Transaction } from './transaction'; @Entity() export class Order { @@ -9,24 +18,43 @@ export class Order { @IsNotEmpty() id!: string; - @ManyToOne(() => User, user => user.orders) // <- Correctly reference the User entity and its orders property + @ManyToOne(() => User, user => user.orders) @IsNotEmpty() buyer!: User; - @ManyToOne(() => Product, product => product.orders) // <- Correctly reference the Product entity and its orders property + @OneToMany(() => OrderItem, orderItem => orderItem.order, { cascade: true }) @IsNotEmpty() - product!: Product; + orderItems!: OrderItem[]; @Column('decimal') @IsNotEmpty() @IsNumber() totalPrice!: number; + @OneToMany(() => Transaction, transaction => transaction.order) + transactions!: Transaction[]; + @Column({ default: 'order placed' }) + @IsNotEmpty() + @IsIn([ + 'order placed', + 'cancelled', + 'awaiting shipment', + 'in transit', + 'delivered', + 'received', + 'returned', + 'completed', + ]) + orderStatus!: string; + @Column('int') @IsNotEmpty() @IsNumber() quantity!: number; + @Column({ default: 'City, Country street address' }) + address!: string; + @Column() @IsDate() @IsNotEmpty() diff --git a/src/entities/OrderItem.ts b/src/entities/OrderItem.ts new file mode 100644 index 0000000..130b330 --- /dev/null +++ b/src/entities/OrderItem.ts @@ -0,0 +1,29 @@ +import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'; +import { IsNotEmpty, IsNumber } from 'class-validator'; +import { Order } from './Order'; +import { Product } from './Product'; + +@Entity() +export class OrderItem { + @PrimaryGeneratedColumn('uuid') + @IsNotEmpty() + id!: string; + + @ManyToOne(() => Order, order => order.orderItems) + @IsNotEmpty() + order!: Order; + + @ManyToOne(() => Product, product => product.orderItems) + @IsNotEmpty() + product!: Product; + + @Column('decimal') + @IsNotEmpty() + @IsNumber() + price!: number; + + @Column('int') + @IsNotEmpty() + @IsNumber() + quantity!: number; +} diff --git a/src/entities/Product.ts b/src/entities/Product.ts index 82603a4..e144a04 100644 --- a/src/entities/Product.ts +++ b/src/entities/Product.ts @@ -9,16 +9,21 @@ import { ManyToMany, OneToMany, JoinTable, + OneToOne, + JoinColumn, } from 'typeorm'; import { IsNotEmpty, IsString, IsBoolean, ArrayNotEmpty, IsArray, MaxLength } from 'class-validator'; import { User } from './User'; import { Category } from './Category'; import { Order } from './Order'; +import { Coupon } from './coupon'; +import { OrderItem } from './OrderItem'; +import { VendorOrderItem } from './VendorOrderItem'; @Entity() @Unique(['id']) export class Product { - static query() { + static query () { throw new Error('Method not implemented.'); } @PrimaryGeneratedColumn('uuid') @@ -29,8 +34,15 @@ export class Product { @IsNotEmpty() vendor!: User; - @OneToMany(() => Order, (order: any) => order.product) // Specify the inverse side of the relationship - orders!: Order[]; + @OneToMany(() => OrderItem, orderItem => orderItem.product) + orderItems!: OrderItem[]; + + @OneToMany(() => VendorOrderItem, vendorOrderItems => vendorOrderItems.product) + vendorOrderItems!: VendorOrderItem[]; + + @OneToOne(() => Coupon, (coupons: any) => coupons.product) + @JoinColumn() + coupons?: Coupon; @Column() @IsNotEmpty() diff --git a/src/entities/User.ts b/src/entities/User.ts index 2f55da0..ef256d7 100644 --- a/src/entities/User.ts +++ b/src/entities/User.ts @@ -1,3 +1,4 @@ + import { Entity, PrimaryGeneratedColumn, @@ -11,6 +12,7 @@ import { import { IsEmail, IsNotEmpty, IsString, IsBoolean, IsIn } from 'class-validator'; import { roles } from '../utils/roles'; import { Order } from './Order'; +import { Transaction } from './transaction'; export interface UserInterface { id?: string; @@ -98,14 +100,20 @@ export class User { @OneToMany(() => Order, (order: any) => order.buyer) orders!: Order[]; + @OneToMany(() => Transaction, transaction => transaction.user) + transactions!: Transaction[]; + @CreateDateColumn() createdAt!: Date; @UpdateDateColumn() updatedAt!: Date; + @Column({ type: 'numeric', precision: 24, scale: 2, default: 0 }) + accountBalance!: number; + @BeforeInsert() - setRole(): void { + setRole (): void { this.role = this.userType === 'Vendor' ? roles.vendor : roles.buyer; } } diff --git a/src/entities/VendorOrderItem.ts b/src/entities/VendorOrderItem.ts new file mode 100644 index 0000000..9137f6d --- /dev/null +++ b/src/entities/VendorOrderItem.ts @@ -0,0 +1,30 @@ +import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'; +import { IsNotEmpty, IsNumber } from 'class-validator'; +import { Order } from './Order'; +import { Product } from './Product'; +import { VendorOrders } from './vendorOrders'; + +@Entity() +export class VendorOrderItem { + @PrimaryGeneratedColumn('uuid') + @IsNotEmpty() + 'id'!: string; + + @ManyToOne(() => VendorOrders, order => order.vendorOrderItems) + @IsNotEmpty() + 'order'!: VendorOrders; + + @ManyToOne(() => Product, product => product.vendorOrderItems) + @IsNotEmpty() + 'product'!: Product; + + @Column('decimal') + @IsNotEmpty() + @IsNumber() + 'price/unit'!: number; + + @Column('int') + @IsNotEmpty() + @IsNumber() + 'quantity'!: number; +} diff --git a/src/entities/coupon.ts b/src/entities/coupon.ts new file mode 100644 index 0000000..39631c3 --- /dev/null +++ b/src/entities/coupon.ts @@ -0,0 +1,68 @@ +import { + Entity, + PrimaryGeneratedColumn, + Column, + Unique, + CreateDateColumn, + UpdateDateColumn, + ManyToOne, + JoinColumn, +} from 'typeorm'; +import { IsDate, IsNotEmpty, IsArray, IsIn } from 'class-validator'; +import { User } from './User'; +import { Product } from './Product'; + +@Entity() +@Unique(['id']) +@Unique(['code']) // Ensure only 'code' is unique +export class Coupon { + @PrimaryGeneratedColumn('uuid') + @IsNotEmpty() + id!: string; + + @ManyToOne(() => User) + @IsNotEmpty() + @JoinColumn() + vendor!: User; + + @ManyToOne(() => Product, product => product.coupons) + @IsNotEmpty() + @JoinColumn() + product!: Product; + + @Column() + @IsNotEmpty() + code!: string; + + @Column() + @IsNotEmpty() + @IsIn(['percentage', 'money']) + discountType!: 'percentage' | 'money'; + + @Column('float') + @IsNotEmpty() + discountRate!: number; + + @Column('timestamp', { nullable: false }) + @IsNotEmpty() + @IsDate() + expirationDate?: Date; + + @Column('int', { default: 0 }) + @IsNotEmpty() + usageTimes!: number; + + @Column('int') + @IsNotEmpty() + maxUsageLimit!: number; + + @Column('simple-array', { nullable: true, default: '' }) + @IsArray() + usedBy!: string[]; + + @CreateDateColumn() + createdAt!: Date; + + @UpdateDateColumn() + updatedAt!: Date; +} diff --git a/src/entities/transaction.ts b/src/entities/transaction.ts new file mode 100644 index 0000000..d475812 --- /dev/null +++ b/src/entities/transaction.ts @@ -0,0 +1,61 @@ +import { + Entity, + PrimaryGeneratedColumn, + Column, + ManyToOne, + JoinColumn, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm'; +import { IsNotEmpty, IsString, IsNumber } from 'class-validator'; +import { User } from './User'; +import { Order } from './Order'; +import { Product } from './Product'; // Assuming Product entity exists + +@Entity() +export class Transaction { + @PrimaryGeneratedColumn('uuid') + @IsNotEmpty() + id!: string; + + @ManyToOne(() => User, { nullable: false }) + @JoinColumn({ name: 'userId' }) + user!: User; + + @ManyToOne(() => Order, { nullable: true }) + @JoinColumn({ name: 'orderId' }) + order?: Order; + + @ManyToOne(() => Product, { nullable: true }) + @JoinColumn({ name: 'productId' }) + product?: Product; + + @Column({ type: 'numeric', precision: 15, scale: 2, default: 0 }) + @IsNotEmpty() + @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() + type!: 'debit' | 'credit'; + + @Column({ type: 'text', nullable: true }) + description?: string; + + @CreateDateColumn() + createdAt!: Date; + + @UpdateDateColumn() + updatedAt!: Date; +} \ No newline at end of file diff --git a/src/entities/vendorOrders.ts b/src/entities/vendorOrders.ts new file mode 100644 index 0000000..38269e6 --- /dev/null +++ b/src/entities/vendorOrders.ts @@ -0,0 +1,49 @@ +import { + Entity, + PrimaryGeneratedColumn, + Column, + ManyToOne, + OneToMany, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm'; +import { IsNotEmpty, IsNumber, IsDate, IsIn, isNotEmpty } from 'class-validator'; +import { User } from './User'; +import { OrderItem } from './OrderItem'; +import { Transaction } from './transaction'; +import { Order } from './Order'; +import { VendorOrderItem } from './VendorOrderItem'; + +@Entity() +export class VendorOrders { + @PrimaryGeneratedColumn('uuid') + @IsNotEmpty() + id!: string; + + @ManyToOne(() => User) + @IsNotEmpty() + vendor!: User; + + @OneToMany(() => VendorOrderItem, vendorOrderItems => vendorOrderItems.order, { cascade: true }) + @IsNotEmpty() + vendorOrderItems!: VendorOrderItem[]; + + @ManyToOne(() => Order) + @IsNotEmpty() + order!: Order; + + @Column('decimal') + @IsNotEmpty() + @IsNumber() + totalPrice!: number; + + @Column({ default: 'pending' }) + @IsIn(['pending', 'is-accepted', 'in-transit', 'cancelled', 'delivered', 'completed']) + orderStatus!: string; + + @CreateDateColumn() + createdAt!: Date; + + @UpdateDateColumn() + updatedAt!: Date; +} diff --git a/src/entities/wishList.ts b/src/entities/wishList.ts index 69dbebd..7f74023 100644 --- a/src/entities/wishList.ts +++ b/src/entities/wishList.ts @@ -1,10 +1,19 @@ -import { Entity, PrimaryGeneratedColumn, BaseEntity,Column, Unique, ManyToOne, CreateDateColumn, UpdateDateColumn,} from "typeorm"; +import { + Entity, + PrimaryGeneratedColumn, + BaseEntity, + Column, + Unique, + ManyToOne, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm'; import { IsNotEmpty, IsString } from 'class-validator'; import { User } from './User'; -@Entity("wishlist") +@Entity('wishlist') @Unique(['id']) -export class wishList extends BaseEntity{ +export class wishList extends BaseEntity { @PrimaryGeneratedColumn() @IsNotEmpty() id!: number; @@ -23,4 +32,4 @@ export class wishList extends BaseEntity{ @UpdateDateColumn() updatedAt!: Date; -} \ No newline at end of file +} diff --git a/src/helper/couponValidator.ts b/src/helper/couponValidator.ts new file mode 100644 index 0000000..9736aa8 --- /dev/null +++ b/src/helper/couponValidator.ts @@ -0,0 +1,58 @@ +import Joi from 'joi'; +import { Coupon } from '../entities/coupon'; + +export const validateCoupon = ( + coupon: Pick +): Joi.ValidationResult => { + const schema = Joi.object({ + code: Joi.string().min(5).required().messages({ + 'any.required': 'code is required.', + 'string.min': 'code must be at least 5 characters long.', + }), + discountRate: Joi.number().required().messages({ + 'any.required': 'discountRate is required.', + }), + expirationDate: Joi.date().required().messages({ + 'any.required': 'expirationDate is required.', + }), + maxUsageLimit: Joi.number().required().messages({ + 'any.required': 'maxUsageLimit is required.', + }), + discountType: Joi.string().required().messages({ + 'any.required': 'discountType is required.', + }), + product: Joi.string().required().messages({ + 'any.required': 'product is required.', + }), + }); + + return schema.validate(coupon); +}; + +export const validateCouponUpdate = ( + coupon: Partial> +): Joi.ValidationResult => { + const schema = Joi.object({ + code: Joi.string().min(5).messages({ + 'string.min': 'code must be at least 5 characters long.', + }), + discountRate: Joi.number().messages({ + 'number.base': 'discountRate must be a number.', + }), + expirationDate: Joi.date().messages({ + 'date.base': 'expirationDate must be a valid date.', + }), + maxUsageLimit: Joi.number().messages({ + 'number.base': 'maxUsageLimit must be a number.', + }), + discountType: Joi.string().messages({ + 'string.base': 'discountType must be a string.', + }), + }) + .min(1) + .messages({ + 'object.min': 'At least one field must be updated.', + }); + + return schema.validate(coupon); +}; diff --git a/src/index.ts b/src/index.ts index 07efd39..d689c27 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,21 +5,27 @@ import router from './routes'; import { addDocumentation } from './startups/docs'; import 'reflect-metadata'; import cookieParser from 'cookie-parser'; -import session from "express-session"; +import session from 'express-session'; import passport from 'passport'; import { CustomError, errorHandler } from './middlewares/errorHandler'; import morgan from 'morgan'; import { dbConnection } from './startups/dbConnection'; + +import { Server } from 'socket.io'; +import { init as initSocketIO } from './utils/socket'; + dotenv.config(); export const app = express(); const port = process.env.PORT || 8000; -app.use(session({ - secret: 'keyboard cat' -})) -app.use(passport.initialize()) -app.use(passport.session()) +app.use( + session({ + secret: 'keyboard cat', + }) +); +app.use(passport.initialize()); +app.use(passport.session()); app.use(express.json()); app.use(cookieParser()); app.use(cors({ origin: '*' })); @@ -43,3 +49,14 @@ app.use(morgan(morganFormat)); export const server = app.listen(port, () => { console.log(`[server]: Server is running at http://localhost:${port}`); }); + +// Socket.IO setup +const io = initSocketIO(server); + +io.on('connection', socket => { + console.log('Client connected'); + + socket.on('disconnect', () => { + console.log('Client disconnected'); + }); +}); diff --git a/src/routes/ProductRoutes.ts b/src/routes/ProductRoutes.ts index 16dcd69..4a72b47 100644 --- a/src/routes/ProductRoutes.ts +++ b/src/routes/ProductRoutes.ts @@ -1,6 +1,6 @@ import { RequestHandler, Router } from 'express'; -import { productStatus, searchProduct } from '../controllers/index'; +import { productStatus, searchProduct, } from '../controllers/index'; import { hasRole } from '../middlewares/roleCheck'; import upload from '../middlewares/multer'; import { authMiddleware } from '../middlewares/verifyToken'; @@ -14,8 +14,19 @@ import { deleteProduct, getRecommendedProducts, listAllProducts, - singleProduct + singleProduct, + createOrder, + getOrders, + updateOrder, + getOrdersHistory,Payment, + getSingleVendorOrder, + getVendorOrders, + updateVendorOrder, + getBuyerVendorOrders, + getSingleBuyerVendorOrder, + updateBuyerVendorOrder, } from '../controllers'; + const router = Router(); router.get('/all', listAllProducts); router.get('/recommended', authMiddleware as RequestHandler, hasRole('BUYER'), getRecommendedProducts); @@ -29,4 +40,20 @@ router.delete('/images/:id', authMiddleware as RequestHandler, hasRole('VENDOR') router.delete('/:id', authMiddleware as RequestHandler, hasRole('VENDOR'), deleteProduct); router.put('/availability/:id', authMiddleware as RequestHandler, hasRole('VENDOR'), productStatus); +router.post('/orders', authMiddleware as RequestHandler, hasRole('BUYER'), createOrder); +router.get('/client/orders', authMiddleware as RequestHandler, hasRole('BUYER'), getOrders); +router.put('/client/orders/:orderId', authMiddleware as RequestHandler, hasRole('BUYER'), updateOrder); +router.get('/orders/history', authMiddleware as RequestHandler, hasRole('BUYER'), getOrdersHistory); + +// Vendor order management +router.get('/vendor/orders', authMiddleware as RequestHandler, hasRole('VENDOR'), getVendorOrders); +router.get('/vendor/orders/:id', authMiddleware as RequestHandler, hasRole('VENDOR'), getSingleVendorOrder); +router.put('/vendor/orders/:id', authMiddleware as RequestHandler, hasRole('VENDOR'), updateVendorOrder); + +// Admin order management +router.get('/admin/orders', authMiddleware as RequestHandler, hasRole('ADMIN'), getBuyerVendorOrders); +router.get('/admin/orders/:id', authMiddleware as RequestHandler, hasRole('ADMIN'), getSingleBuyerVendorOrder); +router.put('/admin/orders/:id', authMiddleware as RequestHandler, hasRole('ADMIN'), updateBuyerVendorOrder); +router.post('/payment/:id', authMiddleware as RequestHandler, hasRole('BUYER'), Payment) + export default router; diff --git a/src/routes/UserRoutes.ts b/src/routes/UserRoutes.ts index 50bb4ca..79b0551 100644 --- a/src/routes/UserRoutes.ts +++ b/src/routes/UserRoutes.ts @@ -1,7 +1,7 @@ import { Router } from 'express'; import { responseError } from '../utils/response.utils'; import { UserInterface } from '../entities/User'; -import jwt from 'jsonwebtoken' +import jwt from 'jsonwebtoken'; import { disable2FA, enable2FA, @@ -19,7 +19,7 @@ import { activateUser, disactivateUser, userProfileUpdate } from '../controllers import { hasRole } from '../middlewares/roleCheck'; import { isTokenValide } from '../middlewares/isValid'; import passport from 'passport'; -import "../utils/auth"; +import '../utils/auth'; const router = Router(); router.post('/register', userRegistration); @@ -37,35 +37,36 @@ router.post('/password/reset/link', sendPasswordResetLink); router.put('/update', userProfileUpdate); router.get('/google-auth', passport.authenticate('google', { scope: ['profile', 'email'] })); -router.get("/auth/google/callback", - passport.authenticate("google", { - successRedirect: "/user/login/success", - failureRedirect: "/user/login/failed" +router.get( + '/auth/google/callback', + passport.authenticate('google', { + successRedirect: '/user/login/success', + failureRedirect: '/user/login/failed', }) ); -router.get("/login/success", async (req, res) => { +router.get('/login/success', async (req, res) => { const user = req.user as UserInterface; - if(!user){ - responseError(res, 404, 'user not found') + if (!user) { + responseError(res, 404, 'user not found'); } const payload = { id: user?.id, email: user?.email, - role: user?.role - } - const token = jwt.sign(payload, process.env.JWT_SECRET as string,{expiresIn: '24h'}) + role: user?.role, + }; + const token = jwt.sign(payload, process.env.JWT_SECRET as string, { expiresIn: '24h' }); res.status(200).json({ status: 'success', - data:{ - token: token, - message: "Login success" - } - }) + data: { + token: token, + message: 'Login success', + }, + }); }); -router.get("/login/failed", async (req, res) => { +router.get('/login/failed', async (req, res) => { res.status(401).json({ status: false, - message: "Login failed" + message: 'Login failed', }); }); diff --git a/src/routes/couponRoutes.ts b/src/routes/couponRoutes.ts new file mode 100644 index 0000000..3378fbe --- /dev/null +++ b/src/routes/couponRoutes.ts @@ -0,0 +1,22 @@ +import { RequestHandler, Router } from 'express'; +import { + createCoupon, + updateCoupon, + accessAllCoupon, + readCoupon, + deleteCoupon, + buyerApplyCoupon, +} from '../controllers/couponController'; +import { hasRole } from '../middlewares/roleCheck'; +import { authMiddleware } from '../middlewares/verifyToken'; + +const router = Router(); + +router.post('/vendor/:id', authMiddleware as RequestHandler, hasRole('VENDOR'), createCoupon); +router.put('/vendor/:id/update-coupon/:code', authMiddleware as RequestHandler, hasRole('VENDOR'), updateCoupon); +router.get('/vendor/:id/checkout/:code', authMiddleware as RequestHandler, hasRole('VENDOR'), readCoupon); +router.get('/vendor/:id/access-coupons', authMiddleware as RequestHandler, hasRole('VENDOR'), accessAllCoupon); +router.delete('/vendor/:id/checkout/delete', authMiddleware as RequestHandler, hasRole('VENDOR'), deleteCoupon); +router.post('/apply', authMiddleware as RequestHandler, hasRole('BUYER'), buyerApplyCoupon); + +export default router; diff --git a/src/routes/index.ts b/src/routes/index.ts index 3c6edec..6f632d6 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -3,6 +3,7 @@ import { responseSuccess } from '../utils/response.utils'; import userRoutes from './UserRoutes'; import productRoutes from './ProductRoutes'; import wishListRoutes from './wishListRoute'; +import couponRoute from './couponRoutes'; import cartRoutes from './CartRoutes'; const router = Router(); @@ -15,5 +16,6 @@ router.use('/user', userRoutes); router.use('/product', productRoutes); router.use('/wish-list', wishListRoutes); router.use('/cart', cartRoutes); +router.use('/coupons', couponRoute); export default router; diff --git a/src/routes/wishListRoute.ts b/src/routes/wishListRoute.ts index d5ac6fb..ea96e40 100644 --- a/src/routes/wishListRoute.ts +++ b/src/routes/wishListRoute.ts @@ -2,13 +2,30 @@ import { RequestHandler, Router } from 'express'; import { authMiddleware } from '../middlewares/verifyToken'; import { hasRole } from '../middlewares'; import { checkUserStatus } from '../middlewares/isAllowed'; -import { wishlistAddProduct,wishlistRemoveProduct,wishlistGetProducts,wishlistClearAllProducts } from '../controllers/wishListController'; +import { + wishlistAddProduct, + wishlistRemoveProduct, + wishlistGetProducts, + wishlistClearAllProducts, +} from '../controllers/wishListController'; const router = Router(); router.post('/add/:id', authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'), wishlistAddProduct); -router.get('/',authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'),wishlistGetProducts); -router.delete('/delete/:id',authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'),wishlistRemoveProduct); -router.delete('/clearAll',authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'),wishlistClearAllProducts); +router.get('/', authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'), wishlistGetProducts); +router.delete( + '/delete/:id', + authMiddleware as RequestHandler, + checkUserStatus, + hasRole('BUYER'), + wishlistRemoveProduct +); +router.delete( + '/clearAll', + authMiddleware as RequestHandler, + checkUserStatus, + hasRole('BUYER'), + wishlistClearAllProducts +); -export default router; \ No newline at end of file +export default router; diff --git a/src/services/adminOrderServices/readOrder.ts b/src/services/adminOrderServices/readOrder.ts new file mode 100644 index 0000000..4bb20f0 --- /dev/null +++ b/src/services/adminOrderServices/readOrder.ts @@ -0,0 +1,158 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { responseSuccess, responseError } from '../../utils/response.utils'; +import { VendorOrderItem } from '../../entities/VendorOrderItem'; +import { VendorOrders } from '../../entities/vendorOrders'; +import { Order } from '../../entities/Order'; + +export const getBuyerVendorOrdersService = async (req: Request, res: Response) => { + try { + const vendorOrderRepository = getRepository(VendorOrders); + const orderRepository = getRepository(Order); + + const orders = await orderRepository.find({ + relations: ['buyer', 'orderItems'], + order: { + createdAt: 'DESC', // Order by creation date, most recent first + }, + }); + + if (!orders.length) { + return responseError(res, 200, `There is no pending orders from buyer`, { orders: [] }); + } + + const sanitizedOrdersResponse = []; + + for (const order of orders) { + const vendorOrders = await vendorOrderRepository.find({ + where: { + order: { + id: order.id, + }, + }, + relations: ['vendor', 'vendorOrderItems', 'vendorOrderItems.product'], + order: { + createdAt: 'DESC', // Order by creation date, most recent first + }, + }); + + sanitizedOrdersResponse.push({ + id: order.id, + totalPrice: order.totalPrice, + totalProducts: order.orderItems.length, + orderStatus: order.orderStatus, + address: order.address, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + buyer: { + id: order.buyer.id, + firstName: order.buyer.firstName, + lastName: order.buyer.lastName, + email: order.buyer.lastName, + gender: order.buyer.gender, + phoneNumber: order.buyer.phoneNumber, + photoUrl: order.buyer.photoUrl, + }, + vendors: vendorOrders.map(vendoOrder => ({ + id: vendoOrder.vendor.id, + firstName: vendoOrder.vendor.firstName, + lastName: vendoOrder.vendor.lastName, + email: vendoOrder.vendor.lastName, + gender: vendoOrder.vendor.gender, + phoneNumber: vendoOrder.vendor.phoneNumber, + photoUrl: vendoOrder.vendor.photoUrl, + order: { + id: vendoOrder.id, + totalPrice: vendoOrder.totalPrice, + orderStatus: vendoOrder.orderStatus, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + orderItems: vendoOrder.vendorOrderItems, + }, + })), + }); + } + + responseSuccess(res, 200, 'Orders retrieved successfully', { + totalOrders: orders.length, + orders: sanitizedOrdersResponse, + }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; + +// Get single vendor order info +export const getSingleBuyerVendorOrderService = async (req: Request, res: Response) => { + try { + const orderId = req.params.id; + + const vendorOrderRepository = getRepository(VendorOrders); + const orderRepository = getRepository(Order); + + const order = await orderRepository.findOne({ + where: { + id: orderId, + }, + relations: ['buyer', 'orderItems'], + order: { + createdAt: 'DESC', // Order by creation date, most recent first + }, + }); + + if (!order) { + return responseError(res, 404, `Order Not Found.`); + } + + const vendorOrders = await vendorOrderRepository.find({ + where: { + order: { + id: order.id, + }, + }, + relations: ['vendor', 'vendorOrderItems', 'vendorOrderItems.product'], + }); + + const sanitizedOrderResponse = { + id: order.id, + totalPrice: order.totalPrice, + totalProducts: order.orderItems.length, + orderStatus: order.orderStatus, + address: order.address, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + buyer: { + id: order.buyer.id, + firstName: order.buyer.firstName, + lastName: order.buyer.lastName, + email: order.buyer.lastName, + gender: order.buyer.gender, + phoneNumber: order.buyer.phoneNumber, + photoUrl: order.buyer.photoUrl, + }, + vendors: vendorOrders.map(vendoOrder => ({ + id: vendoOrder.vendor.id, + firstName: vendoOrder.vendor.firstName, + lastName: vendoOrder.vendor.lastName, + email: vendoOrder.vendor.lastName, + gender: vendoOrder.vendor.gender, + phoneNumber: vendoOrder.vendor.phoneNumber, + photoUrl: vendoOrder.vendor.photoUrl, + order: { + id: vendoOrder.id, + totalPrice: vendoOrder.totalPrice, + orderStatus: vendoOrder.orderStatus, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + orderItems: vendoOrder.vendorOrderItems, + }, + })), + }; + + responseSuccess(res, 200, 'Order retrieved successfully', { + order: sanitizedOrderResponse, + }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; diff --git a/src/services/adminOrderServices/updateOrder.ts b/src/services/adminOrderServices/updateOrder.ts new file mode 100644 index 0000000..876160f --- /dev/null +++ b/src/services/adminOrderServices/updateOrder.ts @@ -0,0 +1,107 @@ +import { Request, Response } from 'express'; +import { Not, getRepository } from 'typeorm'; +import { responseSuccess, responseError } from '../../utils/response.utils'; +import { VendorOrderItem } from '../../entities/VendorOrderItem'; +import { VendorOrders } from '../../entities/vendorOrders'; +import { Order } from '../../entities/Order'; +import { getIO } from '../../utils/socket'; + +export const updateBuyerVendorOrderService = async (req: Request, res: Response) => { + try { + const orderId = req.params.id; + + const vendorOrderRepository = getRepository(VendorOrders); + const orderRepository = getRepository(Order); + + const order = await orderRepository.findOne({ + where: { + id: orderId, + }, + relations: ['buyer', 'orderItems'], + }); + + if (!order) { + return responseError(res, 404, `Order Not Found.`); + } + + if (order.orderStatus === 'completed') { + return responseError(res, 409, 'The order has already been completed.'); + } + + if (order.orderStatus !== 'received') { + return responseError(res, 409, 'Order closure failed: The buyer has not received the item yet.'); + } + + const vendorOrders = await vendorOrderRepository.find({ + where: { + order: { + id: order.id, + }, + }, + relations: ['vendor', 'vendorOrderItems', 'vendorOrderItems.product'], + }); + + for (const order of vendorOrders) { + if (order.orderStatus !== 'delivered') { + return responseError(res, 409, 'Order closure failed: Some vendors have not yet delivered items to the buyer.'); + } + } + + // Update Whole Order + + order.orderStatus = 'completed'; + await orderRepository.save(order); + + const updatedVendorOrder = vendorOrders.map(async order => { + order.orderStatus = 'completed'; + await vendorOrderRepository.save(order); + }); + + const sanitizedOrderResponse = { + id: order.id, + totalPrice: order.totalPrice, + totalProducts: order.orderItems.length, + orderStatus: order.orderStatus, + address: order.address, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + buyer: { + id: order.buyer.id, + firstName: order.buyer.firstName, + lastName: order.buyer.lastName, + email: order.buyer.lastName, + gender: order.buyer.gender, + phoneNumber: order.buyer.phoneNumber, + photoUrl: order.buyer.photoUrl, + }, + vendors: vendorOrders.map(vendoOrder => ({ + id: vendoOrder.vendor.id, + firstName: vendoOrder.vendor.firstName, + lastName: vendoOrder.vendor.lastName, + email: vendoOrder.vendor.lastName, + gender: vendoOrder.vendor.gender, + phoneNumber: vendoOrder.vendor.phoneNumber, + photoUrl: vendoOrder.vendor.photoUrl, + order: { + id: vendoOrder.id, + totalPrice: vendoOrder.totalPrice, + orderStatus: vendoOrder.orderStatus, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + orderItems: vendoOrder.vendorOrderItems, + }, + })), + }; + + getIO().emit('orders', { + action: 'admin update', + order: sanitizedOrderResponse, + }); + + responseSuccess(res, 200, 'Order updated successfully', { + order: sanitizedOrderResponse, + }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; diff --git a/src/services/cartServices/createCart.ts b/src/services/cartServices/createCart.ts index 4fa3646..36232a3 100644 --- a/src/services/cartServices/createCart.ts +++ b/src/services/cartServices/createCart.ts @@ -11,8 +11,7 @@ export const createCartService = async (req: Request, res: Response) => { try { const { error } = validateCartItem(req.body); if (error) { - responseError(res, 400, error.details[0].message); - return; + return responseError(res, 400, error.details[0].message); } if (req.body.quantity < 1) { diff --git a/src/services/couponServices/accessAllCoupon.ts b/src/services/couponServices/accessAllCoupon.ts new file mode 100644 index 0000000..9266a44 --- /dev/null +++ b/src/services/couponServices/accessAllCoupon.ts @@ -0,0 +1,37 @@ +import { Request, Response } from 'express'; +import { responseSuccess, responseError, responseServerError } from '../../utils/response.utils'; +import { getRepository } from 'typeorm'; +import { Coupon } from '../../entities/coupon'; +import { User } from '../../entities/User'; + +export const accessAllCouponService = async (req: Request, res: Response) => { + try { + const { id } = req.params; + + // Retrieve the user by id + const userRepository = getRepository(User); + const user = await userRepository.findOne({ where: { id } }); + + if (!user) { + console.log('User not found with id:', id); + return responseError(res, 404, 'User not found'); + } + + // Retrieve all coupons for the user + const couponRepository = getRepository(Coupon); + const coupons = await couponRepository.find({ + where: { vendor: { id: user.id } }, + relations: ['product'], + }); + + if (!coupons.length) { + console.log('No coupons found for user with id:', id); + return responseError(res, 404, 'No coupons found'); + } + + return responseSuccess(res, 200, 'Coupons retrieved successfully', coupons); + } catch (error: any) { + console.log('Error retrieving all coupons:\n', error); + return responseServerError(res, error); + } +}; diff --git a/src/services/couponServices/buyerApplyCoupon.ts b/src/services/couponServices/buyerApplyCoupon.ts new file mode 100644 index 0000000..85762f6 --- /dev/null +++ b/src/services/couponServices/buyerApplyCoupon.ts @@ -0,0 +1,87 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { Coupon } from '../../entities/coupon'; +import { Cart } from '../../entities/Cart'; +import { CartItem } from '../../entities/CartItem'; + +export const buyerApplyCouponService = async (req: Request, res: Response) => { + try { + const { couponCode } = req.body; + + if (!couponCode) return res.status(400).json({ message: 'Coupon Code is required' }); + + const couponRepository = getRepository(Coupon); + const coupon = await couponRepository.findOne({ + where: { code: couponCode }, + relations: ['product'], + }); + + if (!coupon) return res.status(404).json({ message: 'Invalid Coupon Code' }); + + if (coupon) { + if (coupon.expirationDate && coupon.expirationDate < new Date()) { + return res.status(400).json({ message: 'Coupon is expired' }); + } + + if (coupon.usageTimes == coupon.maxUsageLimit) { + return res.status(400).json({ message: 'Coupon Discount Ended' }); + } + } + const couponProductId = coupon.product.id; + + const cartRepository = getRepository(Cart); + let cart = await cartRepository.findOne({ + where: { user: { id: req.user?.id }, isCheckedOut: false }, + relations: ['items', 'items.product'], + }); + + if (!cart) return res.status(400).json({ message: "You don't have a product in cart" }); + + const cartItemRepository = getRepository(CartItem); + const couponCartItem = await cartItemRepository.findOne({ + where: { + cart: { id: cart.id }, + product: { id: couponProductId }, + }, + relations: ['product'], + }); + + if (!couponCartItem) return res.status(404).json({ message: 'No product in Cart with that coupon code' }); + + let amountReducted; + if (coupon.discountType === 'percentage') { + const reduction = (couponCartItem.product.newPrice * coupon.discountRate) / 100; + amountReducted = reduction; + couponCartItem.newPrice = couponCartItem.product.newPrice - reduction; + + await cartItemRepository.save(couponCartItem); + } else { + amountReducted = coupon.discountRate; + couponCartItem.newPrice = couponCartItem.product.newPrice - amountReducted; + await cartItemRepository.save(couponCartItem); + } + + cart = await cartRepository.findOne({ where: { id: cart.id }, relations: ['items', 'items.product'] }); + if (cart) { + cart.updateTotal(); + await cartRepository.save(cart); + } + + coupon.usageTimes += 1; + + if (req.user?.id) { + coupon.usedBy.push(req.user?.id); + } + + await couponRepository.save(coupon); + + return res + .status(200) + .json({ + message: `Coupon Code successfully activated discount on product: ${couponCartItem.product.name}`, + amountDiscounted: amountReducted, + }); + } catch (error) { + return res.status(500).json({ error: 'Internal server error' }); + } +}; diff --git a/src/services/couponServices/createCouponService.ts b/src/services/couponServices/createCouponService.ts new file mode 100644 index 0000000..a824ddf --- /dev/null +++ b/src/services/couponServices/createCouponService.ts @@ -0,0 +1,55 @@ +import { Request, Response } from 'express'; +import { responseSuccess, responseError, responseServerError } from '../../utils/response.utils'; +import { getRepository } from 'typeorm'; +import { Coupon } from '../../entities/coupon'; +import { validateCoupon } from '../../helper/couponValidator'; +import { User } from '../../entities/User'; +import { Product } from '../../entities/Product'; + +export const createCouponService = async (req: Request, res: Response) => { + try { + const { error } = validateCoupon(req.body); + if (error) { + console.log('Validation Error creating coupon:\n', error); + return res.status(400).json({ status: 'error', error: error?.details[0].message }); + } + + const { code, discountRate, expirationDate, maxUsageLimit, discountType, product: productId } = req.body; + const { id: vendorId } = req.params; + + const userRepository = getRepository(User); + const user = await userRepository.findOne({ where: { id: vendorId } }); + if (!user) { + console.log('Error creating coupon: User not found', user); + return responseError(res, 404, 'User not found'); + } + + const productRepository = getRepository(Product); + const product = await productRepository.findOne({ where: { id: productId } }); + if (!product) { + console.log('Error creating coupon: Product not found', product); + return responseError(res, 403, 'Product not found'); + } + + const couponRepository = getRepository(Coupon); + const existingCoupon = await couponRepository.findOne({ where: { code } }); + if (existingCoupon) { + return responseError(res, 402, 'Coupon code already exists'); + } + + const newCoupon = new Coupon(); + newCoupon.code = code; + newCoupon.discountRate = discountRate; + newCoupon.expirationDate = expirationDate; + newCoupon.maxUsageLimit = maxUsageLimit; + newCoupon.discountType = discountType; + newCoupon.product = product; + newCoupon.vendor = user; + + await couponRepository.save(newCoupon); + responseSuccess(res, 201, 'Coupon created successfully'); + } catch (error: any) { + console.log('Error creating coupon:\n', error); + return responseServerError(res, error); + } +}; diff --git a/src/services/couponServices/deleteCoupon.ts b/src/services/couponServices/deleteCoupon.ts new file mode 100644 index 0000000..c984d9e --- /dev/null +++ b/src/services/couponServices/deleteCoupon.ts @@ -0,0 +1,23 @@ +import { Request, Response } from 'express'; +import { responseSuccess, responseError, responseServerError } from '../../utils/response.utils'; +import { getRepository } from 'typeorm'; +import { Coupon } from '../../entities/coupon'; + +export const deleteCouponService = async (req: Request, res: Response) => { + try { + const couponRepository = getRepository(Coupon); + const coupon = await couponRepository.findOne({ where: { code: req.body.code } }); + + if (!coupon) { + console.log('Invalid coupon.'); + return responseError(res, 404, 'Invalid coupon'); + } + + await couponRepository.remove(coupon); + + return responseSuccess(res, 200, 'Coupon deleted successfully'); + } catch (error: any) { + console.log('Error deleting coupon:\n', error); + return responseServerError(res, error); + } +}; diff --git a/src/services/couponServices/readCoupon.ts b/src/services/couponServices/readCoupon.ts new file mode 100644 index 0000000..47e12ea --- /dev/null +++ b/src/services/couponServices/readCoupon.ts @@ -0,0 +1,23 @@ +import { Request, Response } from 'express'; +import { responseSuccess, responseError, responseServerError } from '../../utils/response.utils'; +import { getRepository } from 'typeorm'; +import { Coupon } from '../../entities/coupon'; + +export const readCouponService = async (req: Request, res: Response) => { + try { + const { code } = req.params; + if (!code) return responseError(res, 400, 'coupon code is required'); + + const couponRepository = getRepository(Coupon); + const coupon = await couponRepository.findOne({ where: { code: code } }); + + if (!coupon) { + return responseError(res, 404, 'Invalid coupon'); + } + + return responseSuccess(res, 200, 'Coupon retrieved successfully', coupon); + } catch (error: any) { + console.log('Error retrieving coupon:\n', error); + return responseServerError(res, error); + } +}; diff --git a/src/services/couponServices/updateService.ts b/src/services/couponServices/updateService.ts new file mode 100644 index 0000000..26aeef6 --- /dev/null +++ b/src/services/couponServices/updateService.ts @@ -0,0 +1,59 @@ +import { Coupon } from '../../entities/coupon'; +import { Request, Response } from 'express'; +import { responseSuccess, responseError, responseServerError } from '../../utils/response.utils'; +import { getRepository } from 'typeorm'; +import { validateCouponUpdate } from '../../helper/couponValidator'; +import { Product } from '../../entities/Product'; + +export const updateCouponService = async (req: Request, res: Response) => { + try { + const { code } = req.params; + const { error } = validateCouponUpdate(req.body); + if (error) { + return res.status(400).json({ status: 'error', error: error?.details[0].message }); + } + + const couponRepository = getRepository(Coupon); + const coupon = await couponRepository.findOne({ where: { code } }); + if (coupon) { + if (req.body.code !== undefined) { + const existtCoupon = await couponRepository.findOne({ where: { code: req.body.code } }); + if (existtCoupon) return responseError(res, 400, 'Coupon code already exists'); + if (req.body.code === coupon.code) return responseError(res, 400, 'Coupon code already up to date'); + coupon.code = req.body.code; + } + if (req.body.discountRate !== undefined) { + coupon.discountRate = req.body.discountRate; + } + if (req.body.expirationDate !== undefined) { + coupon.expirationDate = req.body.expirationDate; + } + if (req.body.maxUsageLimit !== undefined) { + coupon.maxUsageLimit = req.body.maxUsageLimit; + } + if (req.body.discountType !== undefined) { + coupon.discountType = req.body.discountType; + } + if (req.body.product !== undefined) { + const { id } = req.body.product; + const productRepository = getRepository(Product); + const product = await productRepository.findOne({ where: { id } }); + if (!product) { + console.log('Error updating coupon: Product not found', product); + return responseError(res, 404, 'Product not found'); + } + + coupon.product = product; + } + + await couponRepository.save(coupon); + return responseSuccess(res, 200, 'Coupon updated successfully', coupon); + } else { + console.log('Error updating coupon: Coupon not found', coupon); + return responseError(res, 404, 'Coupon not found'); + } + } catch (error: any) { + console.log('Error while updating coupon:\n', error); + return responseServerError(res, error); + } +}; diff --git a/src/services/index.ts b/src/services/index.ts index 8f560c3..08bdbe4 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -21,6 +21,7 @@ export * from './productServices/listAllProductsService'; export * from './productServices/productStatus'; export * from './productServices/viewSingleProduct'; export * from './productServices/searchProduct'; +export * from './productServices/payment' // Buyer wishlist services export * from './wishListServices/addProduct'; @@ -33,3 +34,11 @@ export * from './cartServices/createCart'; export * from './cartServices/readCart'; export * from './cartServices/removeProductInCart'; export * from './cartServices/clearCart'; + +// vendor order management +export * from './vendorOrderServices/readVendorOrder'; +export * from './vendorOrderServices/updateVendorOrder'; + +// vendor order management +export * from './adminOrderServices/readOrder'; +export * from './adminOrderServices/updateOrder'; diff --git a/src/services/orderServices/createOrder.ts b/src/services/orderServices/createOrder.ts new file mode 100644 index 0000000..256bf40 --- /dev/null +++ b/src/services/orderServices/createOrder.ts @@ -0,0 +1,186 @@ +import { Request, Response } from 'express'; +import { getRepository, getManager } from 'typeorm'; +import { Order } from '../../entities/Order'; +import { OrderItem } from '../../entities/OrderItem'; +import { Product } from '../../entities/Product'; +import { User } from '../../entities/User'; +import { Cart } from '../../entities/Cart'; +import { Transaction } from '../../entities/transaction'; +import { responseError, sendErrorResponse, sendSuccessResponse } from '../../utils/response.utils'; +import sendMail from '../../utils/sendOrderMail'; +import { VendorOrders } from '../../entities/vendorOrders'; +import { CartItem } from '../../entities/CartItem'; +import { VendorOrderItem } from '../../entities/VendorOrderItem'; + +export const createOrderService = async (req: Request, res: Response) => { + const { cartId, address } = req.body; + const buyerId = req.user?.id; + + try { + const userRepository = getRepository(User); + const productRepository = getRepository(Product); + const cartRepository = getRepository(Cart); + + const buyer = await userRepository.findOne({ where: { id: buyerId } }); + if (!buyer) { + return responseError(res, 404, 'Buyer not found'); + } + + const cart = await cartRepository.findOne({ + where: { + user: { + id: buyerId, + }, + isCheckedOut: false, + }, + relations: ['items', 'items.product', 'user'], + }); + + if (!cart || cart.items.length === 0) { + return sendErrorResponse(res, 400, 'Cart is empty or already checked out'); + } + + let totalPrice = 0; + const orderItems: OrderItem[] = []; + + for (const item of cart.items) { + const product = item.product; + + if (product.quantity < item.quantity) { + return sendErrorResponse(res, 400, `Not enough ${product.name} in stock`); + } + + totalPrice += product.newPrice * item.quantity; + product.quantity -= item.quantity; + + const orderItem = new OrderItem(); + orderItem.product = product; + orderItem.price = product.newPrice; + orderItem.quantity = item.quantity; + 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; + newOrder.orderItems = orderItems; + newOrder.quantity = cart.items.reduce((acc, item) => acc + item.quantity, 0); + newOrder.orderDate = new Date(); + newOrder.address = `${address.country}, ${address.city}, ${address.street}`; + + await getManager().transaction(async transactionalEntityManager => { + for (const item of cart.items) { + const product = item.product; + await transactionalEntityManager.save(Product, product); + } + + await transactionalEntityManager.save(User, buyer); + + await transactionalEntityManager.save(Order, newOrder); + for (const orderItem of orderItems) { + orderItem.order = newOrder; + await transactionalEntityManager.save(OrderItem, orderItem); + } + + const orderTransaction = new Transaction(); + 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); + + cart.isCheckedOut = true; + await transactionalEntityManager.save(Cart, cart); + }); + + const orderResponse = { + fullName: `${newOrder.buyer.firstName} ${newOrder.buyer.lastName}`, + email: newOrder.buyer.email, + products: orderItems.map(item => ({ + name: item.product.name, + newPrice: item.price, + quantity: item.quantity, + })), + totalAmount: newOrder.totalPrice, + quantity: newOrder.quantity, + orderDate: newOrder.orderDate, + address: newOrder.address, + }; + + const message = { + subject: 'Order created successfully', + ...orderResponse, + }; + + await sendMail(message); + + // separate order by each vendor getting order related to his products + await saveVendorRelatedOrder(newOrder, cart.items); + + return sendSuccessResponse(res, 201, 'Order created successfully', orderResponse); + } catch (error) { + return sendErrorResponse(res, 500, (error as Error).message); + } +}; + +const saveVendorRelatedOrder = async (order: Order, CartItem: CartItem[]) => { + try { + for (const item of CartItem) { + const productRepository = getRepository(Product); + + const product = await productRepository.findOne({ + where: { + id: item.product.id, + }, + relations: ['vendor'], + }); + + if (!product) return; + + const orderItem = new VendorOrderItem(); + orderItem.product = product; + orderItem['price/unit'] = product.newPrice; + orderItem.quantity = item.quantity; + + const vendorOrdersRepository = getRepository(VendorOrders); + let vendorOrders = await vendorOrdersRepository.findOne({ + where: { + vendor: { + id: product.vendor.id, + }, + order: { + id: order.id, + }, + }, + relations: ['vendorOrderItems'], + }); + + if (vendorOrders) { + vendorOrders.totalPrice = Number(vendorOrders.totalPrice) + +product.newPrice * +item.quantity; + vendorOrders.vendorOrderItems = [...vendorOrders.vendorOrderItems, orderItem]; + } else { + const newVendorOrders = new VendorOrders(); + newVendorOrders.vendor = product.vendor; + newVendorOrders.vendorOrderItems = [orderItem]; + newVendorOrders.order = order; + newVendorOrders.totalPrice = +product.newPrice * item.quantity; + vendorOrders = newVendorOrders; + } + + await vendorOrdersRepository.save(vendorOrders); + } + } catch (error) { + console.log((error as Error).message); + } +}; \ No newline at end of file diff --git a/src/services/orderServices/getOrderService.ts b/src/services/orderServices/getOrderService.ts new file mode 100644 index 0000000..4208123 --- /dev/null +++ b/src/services/orderServices/getOrderService.ts @@ -0,0 +1,63 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { responseSuccess, responseError } from '../../utils/response.utils'; +import { Order } from '../../entities/Order'; +import { OrderItem } from '../../entities/OrderItem'; + +// Example usage: + +export const getOrdersService = async (req: Request, res: Response) => { + try { + const orderRepository = getRepository(Order); + const buyerId = req.user?.id; + + const orders = await orderRepository.find({ + where: { + buyer: { + id: buyerId, + }, + }, + relations: ['buyer', 'orderItems', 'orderItems.product'], + order: { + createdAt: 'DESC', // Order by creation date, most recent first + }, + }); + + if (!orders || orders.length === 0) { + return responseSuccess(res, 404, `You haven't made any orders yet`, { orders: [] }); + } + + const sanitezedResponse = orders.map(order => ({ + id: order.id, + totalPrice: order.totalPrice, + orderStatus: order.orderStatus, + quantity: order.quantity, + address: order.address, + orderDate: order.orderDate, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + buyer: { + id: order.buyer.id, + firstName: order.buyer.firstName, + lastName: order.buyer.lastName, + accountBalance: order.buyer.accountBalance, + }, + orderItems: order.orderItems.map((item: OrderItem) => ({ + id: item.id, + price: item.price, + quantity: item.quantity, + product: { + id: item.product.id, + name: item.product.name, + description: item.product.description, + images: item.product.images, + price: item.product.newPrice, + expirationDate: item.product.expirationDate, + }, + })), + })); + responseSuccess(res, 200, 'Orders retrieved successfully', { orders: sanitezedResponse }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; diff --git a/src/services/orderServices/getOrderTransactionHistory.ts b/src/services/orderServices/getOrderTransactionHistory.ts new file mode 100644 index 0000000..6bd0b17 --- /dev/null +++ b/src/services/orderServices/getOrderTransactionHistory.ts @@ -0,0 +1,42 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { Transaction } from '../../entities/transaction'; +import { sendErrorResponse, sendSuccessResponse } from '../../utils/response.utils'; +import { OrderItem } from '../../entities/OrderItem'; + +export const getTransactionHistoryService = async (req: Request, res: Response) => { + const userId = req.user?.id; + + try { + const transactionRepository = getRepository(Transaction); + const transactions = await transactionRepository.find({ + where: { user: { id: userId } }, + order: { createdAt: 'DESC' }, + relations: ['order'], + }); + + if (!transactions || transactions.length === 0) { + return sendErrorResponse(res, 404, 'No transaction history found'); + } + + const transactionHistory = transactions.map(transaction => ({ + id: transaction.id, + amount: transaction.amount, + type: transaction.type, + description: transaction.description, + createdAt: transaction.createdAt, + order: transaction.order + ? { + id: transaction.order.id, + totalPrice: transaction.order.totalPrice, + orderDate: transaction.order.orderDate, + address: transaction.order.address, + } + : null, + })); + + return sendSuccessResponse(res, 200, 'Transaction history retrieved successfully', transactionHistory); + } catch (error) { + return sendErrorResponse(res, 500, (error as Error).message); + } +}; diff --git a/src/services/orderServices/updateOrderService.ts b/src/services/orderServices/updateOrderService.ts new file mode 100644 index 0000000..82a043a --- /dev/null +++ b/src/services/orderServices/updateOrderService.ts @@ -0,0 +1,136 @@ +import { Request, Response } from 'express'; +import { getManager, EntityManager, Repository } from 'typeorm'; +import { Order } from '../../entities/Order'; +import { Product } from '../../entities/Product'; +import { User } from '../../entities/User'; +import { OrderItem } from '../../entities/OrderItem'; +import { Transaction } from '../../entities/transaction'; +import { responseError, sendErrorResponse, sendSuccessResponse } from '../../utils/response.utils'; +import sendMail from '../../utils/sendOrderMail'; +interface OrderStatusType { + orderStatus: + | 'order placed' + | 'cancelled' + | 'awaiting shipment' + | 'in transit' + | 'delivered' + | 'received' + | 'returned'; +} +export const updateOrderService = async (req: Request, res: Response) => { + const { orderId } = req.params; + const { orderStatus } = req.body; + + try { + await getManager().transaction(async (transactionalEntityManager: EntityManager) => { + const orderRepository: Repository = transactionalEntityManager.getRepository(Order); + const productRepository: Repository = transactionalEntityManager.getRepository(Product); + const userRepository: Repository = transactionalEntityManager.getRepository(User); + const orderItemRepository: Repository = transactionalEntityManager.getRepository(OrderItem); + const transactionRepository: Repository = transactionalEntityManager.getRepository(Transaction); + + const buyerId = req.user?.id; + if (!buyerId) { + throw new Error('Unauthorized'); + } + + // Fetch order and related entities + const order: Order | null = await orderRepository.findOne({ + where: { id: orderId, buyer: { id: buyerId } }, + relations: ['orderItems', 'orderItems.product', 'buyer'], + }); + + if (!order) { + return sendErrorResponse(res, 404, 'Order not found'); + } + // Check if order can be updated + if (isOrderFinalStatus(order.orderStatus)) { + return sendErrorResponse(res, 401, `Order cannot be updated once it is ${order.orderStatus}`); + } + + // Handle order status transitions + if (orderStatus !== undefined && order.orderStatus !== orderStatus) { + switch (orderStatus) { + case 'cancelled': + case 'returned': + if (order.orderStatus !== 'delivered') { + await processRefund(order, transactionalEntityManager); + } + break; + default: + break; + } + + order.orderStatus = orderStatus; + } + + // Save updated order status + await orderRepository.save(order); + + // Prepare response data + const orderResponse = { + fullName: `${order.buyer.firstName} ${order.buyer.lastName}`, + email: order.buyer.email, + products: order.orderItems.map((item: OrderItem) => ({ + name: item.product.name, + newPrice: item.price, + quantity: item.quantity, + })), + totalAmount: order.totalPrice, + quantity: order.quantity, + orderDate: order.orderDate, + address: order.address, + }; + + // Send email notification + const message = { + subject: 'Order updated successfully', + ...orderResponse, + }; + await sendMail(message); + + // Respond with success + return sendSuccessResponse(res, 200, 'Order updated successfully', orderResponse); + }); + } catch (error) { + console.error('Error updating order:', error); + return sendErrorResponse(res, 500, (error as Error).message); + } +}; + +async function processRefund (order: Order, entityManager: EntityManager) { + const buyer = order.buyer; + + // Refund buyer + const previousBalance = buyer.accountBalance; + buyer.accountBalance += order.totalPrice; + const currentBalance = buyer.accountBalance; + await entityManager.save(buyer); + + // Record refund transaction + const refundTransaction = new Transaction(); + refundTransaction.user = buyer; + refundTransaction.order = order; + refundTransaction.amount = order.totalPrice; + refundTransaction.previousBalance = previousBalance; + refundTransaction.currentBalance = currentBalance; + refundTransaction.type = 'credit'; + refundTransaction.description = 'Refund for cancelled or returned order'; + await entityManager.save(refundTransaction); + + // Return products to store + for (const orderItem of order.orderItems) { + const product = orderItem.product; + product.quantity += orderItem.quantity; + await entityManager.save(product); + } + + // Clear order details + order.orderItems = []; + order.totalPrice = 0; + order.quantity = 0; +} + +function isOrderFinalStatus (status: string): boolean { + return ['cancelled', 'delivered', 'returned', 'completed'].includes(status); +} diff --git a/src/services/productServices/deleteProduct.ts b/src/services/productServices/deleteProduct.ts index 43ec3d1..068c4c9 100644 --- a/src/services/productServices/deleteProduct.ts +++ b/src/services/productServices/deleteProduct.ts @@ -3,30 +3,28 @@ import { Product } from '../../entities/Product'; import { getRepository } from 'typeorm'; import { responseError, responseSuccess } from '../../utils/response.utils'; - export const deleteProductService = async (req: Request, res: Response) => { - try { - const { id } = req.params; - - const productRepository = getRepository(Product); + try { + const { id } = req.params; - const product = await productRepository.findOne({ - where: { - id: id, - vendor: { - id: req.user?.id - } - } - }); + const productRepository = getRepository(Product); - if (product) { - await productRepository.remove(product); - return responseSuccess(res, 200, 'Product successfully deleted'); - } + const product = await productRepository.findOne({ + where: { + id: id, + vendor: { + id: req.user?.id, + }, + }, + }); - return responseError(res, 404, 'Product not found'); - - } catch (error) { - responseError(res, 400, (error as Error).message); + if (product) { + await productRepository.remove(product); + return responseSuccess(res, 200, 'Product successfully deleted'); } + + return responseError(res, 404, 'Product not found'); + } catch (error) { + responseError(res, 400, (error as Error).message); + } }; diff --git a/src/services/productServices/getRecommendedProductsService.ts b/src/services/productServices/getRecommendedProductsService.ts index 19368e1..fde015d 100644 --- a/src/services/productServices/getRecommendedProductsService.ts +++ b/src/services/productServices/getRecommendedProductsService.ts @@ -1,62 +1,63 @@ -import { Request, Response } from "express"; -import { responseError, responseSuccess } from "../../utils/response.utils"; -import { getRepository } from "typeorm"; -import { Product } from "../../entities/Product"; +import { Request, Response } from 'express'; +import { responseError, responseSuccess } from '../../utils/response.utils'; +import { getRepository } from 'typeorm'; +import { Product } from '../../entities/Product'; interface conditionDoc { - categories: any[] | null; - vendor: any | null + categories: any[] | null; + vendor: any | null; } export const getRecommendedProductsService = async (req: Request, res: Response) => { + try { + // Define pagination parameters + const page = req.query.page ? Number(req.query.page) : 1; + const limit = req.query.limit ? Number(req.query.limit) : 10; + const skip = (page - 1) * limit; + const condition: conditionDoc = { + categories: null, + vendor: null, + }; - try { - // Define pagination parameters - const page = req.query.page ? Number(req.query.page) : 1; - const limit = req.query.limit ? Number(req.query.limit) : 10; - const skip = (page - 1) * limit; - const condition: conditionDoc = { - categories: null, - vendor: null - }; - - if (req.query.categories) { - const categoryIds = Array.isArray(req.query.categories) ? req.query.categories : [req.query.categories]; - condition.categories = categoryIds; - }; - if (req.query.vendor) condition.vendor = req.query.vendor; + if (req.query.categories) { + const categoryIds = Array.isArray(req.query.categories) ? req.query.categories : [req.query.categories]; + condition.categories = categoryIds; + } + if (req.query.vendor) condition.vendor = req.query.vendor; - const productRepository = getRepository(Product); - const productsQuery = productRepository.createQueryBuilder("product") - .leftJoinAndSelect("product.categories", "category") - .leftJoinAndSelect("product.vendor", "vendor") - .where("1 = 1"); + const productRepository = getRepository(Product); + const productsQuery = productRepository + .createQueryBuilder('product') + .leftJoinAndSelect('product.categories', 'category') + .leftJoinAndSelect('product.vendor', 'vendor') + .where('1 = 1'); - if (condition.categories && condition.categories.length > 0) { - productsQuery.andWhere("category.id IN (:...categories)", { categories: condition.categories }); - } - if (condition.vendor) { - productsQuery.andWhere("vendor.id = :vendorId", { vendorId: condition.vendor }); - } + if (condition.categories && condition.categories.length > 0) { + productsQuery.andWhere('category.id IN (:...categories)', { categories: condition.categories }); + } + if (condition.vendor) { + productsQuery.andWhere('vendor.id = :vendorId', { vendorId: condition.vendor }); + } - const products = await productsQuery - .skip(skip) - .take(limit) - .getMany(); - if (products.length < 1) { - return responseSuccess(res, 200, `No products found for the specified ${condition.vendor ? 'vendor' : 'category'}`); - } - const sanitizedProducts = products.map(product => ({ - ...product, - vendor: { - firstName: product.vendor.firstName, - lastName: product.vendor.lastName, - phoneNumber: product.vendor.phoneNumber, - photoUrl: product.vendor.photoUrl - } - })); - return responseSuccess(res, 200, 'Products retrieved', { products: sanitizedProducts }); - } catch (error) { - return responseError(res, 400, (error as Error).message); + const products = await productsQuery.skip(skip).take(limit).getMany(); + if (products.length < 1) { + return responseSuccess( + res, + 200, + `No products found for the specified ${condition.vendor ? 'vendor' : 'category'}` + ); } -}; \ No newline at end of file + const sanitizedProducts = products.map(product => ({ + ...product, + vendor: { + firstName: product.vendor.firstName, + lastName: product.vendor.lastName, + phoneNumber: product.vendor.phoneNumber, + photoUrl: product.vendor.photoUrl, + }, + })); + return responseSuccess(res, 200, 'Products retrieved', { products: sanitizedProducts }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; diff --git a/src/services/productServices/listAllProductsService.ts b/src/services/productServices/listAllProductsService.ts index 8950abd..f39c7bb 100644 --- a/src/services/productServices/listAllProductsService.ts +++ b/src/services/productServices/listAllProductsService.ts @@ -5,38 +5,40 @@ import { responseError, responseSuccess } from '../../utils/response.utils'; import { validate } from 'uuid'; export const listAllProductsService = async (req: Request, res: Response) => { - try { - const page = req.query.page ? Number(req.query.page) : 1; - const limit = req.query.limit ? Number(req.query.limit) : 10; - const skip = (page - 1) * limit; - const category = req.query.category ; + try { + const page = req.query.page ? Number(req.query.page) : 1; + const limit = req.query.limit ? Number(req.query.limit) : 10; + const skip = (page - 1) * limit; + const category = req.query.category; - - const productRepository = getRepository(Product); - const products = await productRepository.find({ - where: { - categories: { - name: category as string - } - }, - skip, - take: limit, - relations: ["categories","vendor"], - select: { - vendor: { - id: true, firstName: true, lastName: true, - email: true, phoneNumber: true, photoUrl: true - } - } - } - ); + const productRepository = getRepository(Product); + const products = await productRepository.find({ + where: { + categories: { + name: category as string, + }, + }, + skip, + take: limit, + relations: ['categories', 'vendor'], + select: { + vendor: { + id: true, + firstName: true, + lastName: true, + email: true, + phoneNumber: true, + photoUrl: true, + }, + }, + }); - if (products.length < 1) { - return responseSuccess(res, 200, 'No products found'); - } - - return responseSuccess(res, 200, 'Products retrieved', { products }); - } catch (error) { - responseError(res, 400, (error as Error).message); + if (products.length < 1) { + return responseSuccess(res, 200, 'No products found'); } + + return responseSuccess(res, 200, 'Products retrieved', { products }); + } catch (error) { + responseError(res, 400, (error as Error).message); + } }; diff --git a/src/services/productServices/payment.ts b/src/services/productServices/payment.ts new file mode 100644 index 0000000..b613296 --- /dev/null +++ b/src/services/productServices/payment.ts @@ -0,0 +1,52 @@ +import { Request, Response } from 'express'; +import { Cart } from '../../entities/Cart'; // Import your Cart entity +import { Order } from '../../entities/Order'; // Import your Order entity +import { getRepository, getTreeRepository } from 'typeorm'; +import dotenv from 'dotenv'; +import Stripe from 'stripe'; +dotenv.config(); +const stripeInstance = new Stripe(process.env.STRIPE_SECRET_KEY as string, { + apiVersion: "2024-04-10", +}); + +export const confirmPayment = async (req: Request, res: Response) => { + try { + const { payment_method } = req.body; + const cartId = req.params.cartId; // Get the cart ID from the params + + const cartRepository = getRepository(Cart); + const orderRepository = getTreeRepository(Order) + const cart = await cartRepository.findOne({where: {id : cartId}}); + if (!cart) { + return res.status(404).json({ error: 'Cart not found.' }); + } + const order = await orderRepository.findOne({ where: { buyer: cart.user } }); + if (!order) { + return res.status(404).json({ error: 'order not found.' }); + } + + const paymentIntent = await stripeInstance.paymentIntents.create({ + amount: cart.totalAmount, // Convert total to cents + currency: 'usd', + description: `Order #${cartId}`, + return_url: 'https://frontend-website.com/success', + confirm: true, + payment_method, + }); + + order.orderStatus = 'awaiting shipment'; + await orderRepository.save(order); + + + if (paymentIntent.status === 'succeeded') { + // Payment succeeded + res.status(200).json({ message: 'Payment successful!' }); + } else { + // Payment failed + res.status(400).json({ error: 'Payment failed.' }); + } + } catch (error) { + console.error('Error confirming payment:', error); + res.status(500).json({ error: 'Something went wrong' }); + } +}; \ No newline at end of file diff --git a/src/services/productServices/readProduct.ts b/src/services/productServices/readProduct.ts index 5c9257c..b3c244d 100644 --- a/src/services/productServices/readProduct.ts +++ b/src/services/productServices/readProduct.ts @@ -4,67 +4,75 @@ import { getRepository } from 'typeorm'; import { responseError, responseSuccess } from '../../utils/response.utils'; export const readProductsService = async (req: Request, res: Response) => { - try { - // Define pagination parameters - const page = req.query.page ? Number(req.query.page) : 1; - const limit = req.query.limit ? Number(req.query.limit) : 10; - const skip = (page - 1) * limit; + try { + // Define pagination parameters + const page = req.query.page ? Number(req.query.page) : 1; + const limit = req.query.limit ? Number(req.query.limit) : 10; + const skip = (page - 1) * limit; - // Retrieve products - const productRepository = getRepository(Product); - const products = await productRepository.find({ - where: { - vendor: { - id: req.user?.id, - }, - }, - skip, - take: limit, - relations: ['categories', 'vendor'], - select: { - vendor: { - id: true, firstName: true, lastName: true, - email: true, phoneNumber: true, photoUrl: true - } - } - }); + // Retrieve products + const productRepository = getRepository(Product); + const products = await productRepository.find({ + where: { + vendor: { + id: req.user?.id, + }, + }, + skip, + take: limit, + relations: ['categories', 'vendor'], + select: { + vendor: { + id: true, + firstName: true, + lastName: true, + email: true, + phoneNumber: true, + photoUrl: true, + }, + }, + }); - if (products.length < 1) { - return responseSuccess(res, 200, 'You have no products yet'); - } - return responseSuccess(res, 200, 'Products retrieved', { products }); - } catch (error) { - responseError(res, 400, (error as Error).message); + if (products.length < 1) { + return responseSuccess(res, 200, 'You have no products yet'); } + return responseSuccess(res, 200, 'Products retrieved', { products }); + } catch (error) { + responseError(res, 400, (error as Error).message); + } }; export const readProductService = async (req: Request, res: Response) => { - try { - const { id } = req.params; + try { + const { id } = req.params; - const productRepository = getRepository(Product); - const product = await productRepository.findOne({ - where: { - id: id, - vendor: { - id: req.user?.id, - }, - }, - relations: ['categories', 'vendor'], - select: { - vendor: { - id: true, firstName: true, lastName: true, - email: true, phoneNumber: true, photoUrl: true - } - } - }); + const productRepository = getRepository(Product); + const product = await productRepository.findOne({ + where: { + id: id, + vendor: { + id: req.user?.id, + }, + }, + relations: ['categories', 'vendor'], + select: { + vendor: { + id: true, + firstName: true, + lastName: true, + email: true, + phoneNumber: true, + photoUrl: true, + }, + }, + }); - if (!product) { - return responseError(res, 404, 'Product not found'); - } - - return responseSuccess(res, 200, 'Product retrieved', { product }); - } catch (error) { - responseError(res, 400, (error as Error).message); + if (!product) { + return responseError(res, 404, 'Product not found'); } + + return responseSuccess(res, 200, 'Product retrieved', { product }); + } catch (error) { + responseError(res, 400, (error as Error).message); + } }; diff --git a/src/services/productServices/removeProductImage.ts b/src/services/productServices/removeProductImage.ts index 2995593..4424676 100644 --- a/src/services/productServices/removeProductImage.ts +++ b/src/services/productServices/removeProductImage.ts @@ -21,7 +21,7 @@ export const removeProductImageService = async (req: Request, res: Response) => const product = await productRepository.findOne({ where: { id, - vendor: { id: req.user?.id } + vendor: { id: req.user?.id }, }, relations: ['vendor'], }); diff --git a/src/services/productServices/searchProduct.ts b/src/services/productServices/searchProduct.ts index 765f431..9f33b5f 100644 --- a/src/services/productServices/searchProduct.ts +++ b/src/services/productServices/searchProduct.ts @@ -1,4 +1,4 @@ -import { Request, Response } from "express"; +import { Request, Response } from 'express'; import { getRepository, Like } from 'typeorm'; import { Product } from '../../entities/Product'; @@ -26,10 +26,7 @@ export const searchProductService = async (params: SearchProductParams) => { const skip = (page - 1) * limit; - const [products, total] = await query - .skip(skip) - .take(limit) - .getManyAndCount(); + const [products, total] = await query.skip(skip).take(limit).getManyAndCount(); const totalPages = Math.ceil(total / limit); diff --git a/src/services/productServices/viewSingleProduct.ts b/src/services/productServices/viewSingleProduct.ts index f956625..29ac167 100644 --- a/src/services/productServices/viewSingleProduct.ts +++ b/src/services/productServices/viewSingleProduct.ts @@ -4,35 +4,28 @@ import { getRepository } from 'typeorm'; import { responseError } from '../../utils/response.utils'; import { validate } from 'uuid'; - - export const viewSingleProduct = async (req: Request, res: Response) => { - try { - const productId = req.params.id; + try { + const productId = req.params.id; + + if (!validate(productId)) { + return res.status(400).json({ status: 'error', message: 'Invalid product ID' }); + } + if (productId) { + const products = getRepository(Product); + const product = await products.findOneBy({ id: productId }); - if (!validate(productId)) { - return res.status(400).json({ status: 'error', message: 'Invalid product ID' }); - + if (!product) { + return res.status(404).send({ status: 'error', message: 'Product not found' }); } - if(productId){ - const products = getRepository(Product); - const product = await products.findOneBy({ id: productId }); - - if (!product) { - return res.status(404).send({status:'error', message: 'Product not found'}); - - } - - if (product.expirationDate && new Date(product.expirationDate) < new Date()) { - return res.status(400).json({ status: 'error', message: 'Product expired' }); - - } - res.status(200).json({ status: 'success', product: product }); + if (product.expirationDate && new Date(product.expirationDate) < new Date()) { + return res.status(400).json({ status: 'error', message: 'Product expired' }); } - - } catch (error) { - console.error('Error handling request:', error); - res.status(500).send('Error fetching product details'); + res.status(200).json({ status: 'success', product: product }); } -} \ No newline at end of file + } catch (error) { + console.error('Error handling request:', error); + res.status(500).send('Error fetching product details'); + } +}; diff --git a/src/services/vendorOrderServices/readVendorOrder.ts b/src/services/vendorOrderServices/readVendorOrder.ts new file mode 100644 index 0000000..feec0c3 --- /dev/null +++ b/src/services/vendorOrderServices/readVendorOrder.ts @@ -0,0 +1,119 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { responseSuccess, responseError } from '../../utils/response.utils'; +import { VendorOrderItem } from '../../entities/VendorOrderItem'; +import { VendorOrders } from '../../entities/vendorOrders'; + +export const getVendorOrdersService = async (req: Request, res: Response) => { + try { + const vendorOrderRepository = getRepository(VendorOrders); + const vendorId = req.user?.id; + + const vendorOrders = await vendorOrderRepository.find({ + where: { + vendor: { + id: vendorId, + }, + }, + relations: ['vendor', 'order.buyer', 'vendorOrderItems', 'vendorOrderItems.product'], + order: { + createdAt: 'DESC', // Order by creation date, most recent first + }, + }); + + if (!vendorOrders.length) { + return responseError(res, 200, `You don't have any pending orders from buyer`, { orders: [] }); + } + + const sanitizedOrderResponse = vendorOrders.map(order => ({ + id: order.id, + totalPrice: order.totalPrice, + orderStatus: order.orderStatus, + address: order.order.address, + createdAt: order.createdAt, + updatedAt: order.updatedAt, + vendor: { + id: order.vendor.id, + firstName: order.vendor.firstName, + lastName: order.vendor.lastName, + email: order.vendor.email, + gender: order.vendor.gender, + phoneNumber: order.vendor.phoneNumber, + photoUrl: order.vendor.photoUrl, + }, + buyer: { + id: order.order.buyer.id, + firstName: order.order.buyer.firstName, + lastName: order.order.buyer.lastName, + email: order.order.buyer.lastName, + gender: order.order.buyer.gender, + phoneNumber: order.order.buyer.phoneNumber, + photoUrl: order.order.buyer.photoUrl, + }, + vendorOrderItems: order.vendorOrderItems, + })); + responseSuccess(res, 200, 'Orders retrieved successfully', { + orders: sanitizedOrderResponse, + }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; + +// Get single vendor order info +export const getSingleVendorOrderService = async (req: Request, res: Response) => { + try { + const vendorOrderId = req.params.id; + + const vendorOrderRepository = getRepository(VendorOrders); + const vendorId = req.user?.id; + + const vendorOrder = await vendorOrderRepository.findOne({ + where: { + id: vendorOrderId, + vendor: { + id: vendorId, + }, + }, + relations: ['vendor', 'order.buyer', 'vendorOrderItems', 'vendorOrderItems.product'], + }); + + if (!vendorOrder) { + return responseError(res, 404, `Order Not Found.`); + } + + const sanitizedOrderResponse = { + id: vendorOrder.id, + totalPrice: vendorOrder.totalPrice, + orderStatus: vendorOrder.orderStatus, + address: vendorOrder.order.address, + createdAt: vendorOrder.createdAt, + updatedAt: vendorOrder.updatedAt, + vendor: { + id: vendorOrder.vendor.id, + firstName: vendorOrder.vendor.firstName, + lastName: vendorOrder.vendor.lastName, + email: vendorOrder.vendor.email, + gender: vendorOrder.vendor.gender, + phoneNumber: vendorOrder.vendor.phoneNumber, + photoUrl: vendorOrder.vendor.photoUrl, + }, + buyer: { + id: vendorOrder.order.buyer.id, + firstName: vendorOrder.order.buyer.firstName, + lastName: vendorOrder.order.buyer.lastName, + email: vendorOrder.order.buyer.lastName, + gender: vendorOrder.order.buyer.gender, + phoneNumber: vendorOrder.order.buyer.phoneNumber, + photoUrl: vendorOrder.order.buyer.photoUrl, + }, + vendorOrderItems: vendorOrder.vendorOrderItems, + }; + + responseSuccess(res, 200, 'Order retrieved successfully', { + order: sanitizedOrderResponse, + }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; diff --git a/src/services/vendorOrderServices/updateVendorOrder.ts b/src/services/vendorOrderServices/updateVendorOrder.ts new file mode 100644 index 0000000..cae2c60 --- /dev/null +++ b/src/services/vendorOrderServices/updateVendorOrder.ts @@ -0,0 +1,87 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { OrderItem } from '../../entities/OrderItem'; +import { responseError, responseSuccess } from '../../utils/response.utils'; +import sendMail from '../../utils/sendOrderMail'; +import { VendorOrders } from '../../entities/vendorOrders'; +import { getIO } from '../../utils/socket'; + +export const updateVendorOrderService = async (req: Request, res: Response) => { + try { + const vendorOrderId = req.params.id; + const { orderStatus } = req.body; + if ( + !['pending', 'is-accepted', 'in-transit', 'cancelled', 'delivered'].includes( + (orderStatus as string).toLowerCase() + ) + ) { + return responseError(res, 400, `Please provide one of defined statuses.`); + } + + const vendorOrderRepository = getRepository(VendorOrders); + const vendorId = req.user?.id; + + const vendorOrder = await vendorOrderRepository.findOne({ + where: { + id: vendorOrderId, + vendor: { + id: vendorId, + }, + }, + relations: ['vendor', 'order.buyer', 'vendorOrderItems', 'vendorOrderItems.product'], + }); + + if (!vendorOrder) { + return responseError(res, 404, `Order Not Found.`); + } + + // Check if order can be updated + if (['delivered', 'cancelled', 'completed'].includes(vendorOrder.orderStatus)) { + return responseError(res, 409, `Order cannot be updated once it is marked as ${vendorOrder.orderStatus}`); + } + + vendorOrder.orderStatus = (orderStatus as string).toLowerCase(); + + // Save updated order status + const updatedVendorOrder = await vendorOrderRepository.save(vendorOrder); + + const sanitizedOrderResponse = { + id: updatedVendorOrder.id, + totalPrice: updatedVendorOrder.totalPrice, + orderStatus: updatedVendorOrder.orderStatus, + address: updatedVendorOrder.order.address, + createdAt: updatedVendorOrder.createdAt, + updatedAt: updatedVendorOrder.updatedAt, + vendor: { + id: updatedVendorOrder.vendor.id, + firstName: updatedVendorOrder.vendor.firstName, + lastName: updatedVendorOrder.vendor.lastName, + email: updatedVendorOrder.vendor.email, + gender: updatedVendorOrder.vendor.gender, + phoneNumber: updatedVendorOrder.vendor.phoneNumber, + photoUrl: updatedVendorOrder.vendor.photoUrl, + }, + buyer: { + id: updatedVendorOrder.order.buyer.id, + firstName: updatedVendorOrder.order.buyer.firstName, + lastName: updatedVendorOrder.order.buyer.lastName, + email: updatedVendorOrder.order.buyer.lastName, + gender: updatedVendorOrder.order.buyer.gender, + phoneNumber: updatedVendorOrder.order.buyer.phoneNumber, + photoUrl: updatedVendorOrder.order.buyer.photoUrl, + }, + vendorOrderItems: updatedVendorOrder.vendorOrderItems, + }; + + getIO().emit('orders', { + action: 'vendor update', + order: sanitizedOrderResponse, + }); + + return responseSuccess(res, 200, 'Order updated successfully', { + order: sanitizedOrderResponse, + }); + } catch (error) { + return responseError(res, 400, (error as Error).message); + } +}; diff --git a/src/services/wishListServices/addProduct.ts b/src/services/wishListServices/addProduct.ts index da3db89..79d0a38 100644 --- a/src/services/wishListServices/addProduct.ts +++ b/src/services/wishListServices/addProduct.ts @@ -4,57 +4,54 @@ import { getRepository } from 'typeorm'; import { wishList } from '../../entities/wishList'; import { Product } from '../../entities/Product'; -export const addProductService = async (req:Request,res:Response)=>{ - try { +export const addProductService = async (req: Request, res: Response) => { + try { + const id = req.params.id; + const wishListRepository = getRepository(wishList); + const productRepository = getRepository(Product); - const id = req.params.id; - const wishListRepository = getRepository(wishList); - const productRepository = getRepository(Product); + const product = await productRepository.findOne({ where: { id } }); + if (!product) { + return res.status(404).json({ message: 'Product not found' }); + } - const product = await productRepository.findOne({where: { id }}); - - if(!product){ - return res.status(404).json({message: "Product not found"}); - } - - const productDetails = { - productId: product.id, - name: product.name, - image: product.images, - newPrice: product.newPrice, - vendorId: product.vendor - } - - const alreadyIn = await wishListRepository.findOne({where: {productId: id, buyer:{ id: req.user?.id} }}) - - if(alreadyIn){ - return res.status(401).json({ - data: { - message: 'Product Already in the wish list', - wishlistAdded: alreadyIn, - product: productDetails, - }, - }) - } - - const addNewProduct = new wishList(); - addNewProduct.productId = id; - addNewProduct.buyer = req.user as User; - - await wishListRepository.save(addNewProduct); + const productDetails = { + productId: product.id, + name: product.name, + image: product.images, + newPrice: product.newPrice, + vendorId: product.vendor, + }; - addNewProduct.buyer = { id: addNewProduct.buyer.id } as unknown as User; + const alreadyIn = await wishListRepository.findOne({ where: { productId: id, buyer: { id: req.user?.id } } }); - return res.status(201).json({ + if (alreadyIn) { + return res.status(401).json({ data: { - message: 'Product Added to wish list', - wishlistAdded: addNewProduct, + message: 'Product Already in the wish list', + wishlistAdded: alreadyIn, product: productDetails, - }, - }); - - } catch (error) { - return res.status(500).json({ error: 'Internal server error' }); + }, + }); } -} \ No newline at end of file + + const addNewProduct = new wishList(); + addNewProduct.productId = id; + addNewProduct.buyer = req.user as User; + + await wishListRepository.save(addNewProduct); + + addNewProduct.buyer = { id: addNewProduct.buyer.id } as unknown as User; + + return res.status(201).json({ + data: { + message: 'Product Added to wish list', + wishlistAdded: addNewProduct, + product: productDetails, + }, + }); + } catch (error) { + return res.status(500).json({ error: 'Internal server error' }); + } +}; diff --git a/src/services/wishListServices/clearAll.ts b/src/services/wishListServices/clearAll.ts index 88af3c6..7299454 100644 --- a/src/services/wishListServices/clearAll.ts +++ b/src/services/wishListServices/clearAll.ts @@ -2,19 +2,18 @@ import { Request, Response } from 'express'; import { getRepository } from 'typeorm'; import { wishList } from '../../entities/wishList'; -export const clearAllProductService = async (req:Request,res:Response)=>{ - try { - const wishListRepository = getRepository(wishList); - const productsForBuyer = await wishListRepository.find({where: { buyer:{ id: req.user?.id} }}); +export const clearAllProductService = async (req: Request, res: Response) => { + try { + const wishListRepository = getRepository(wishList); + const productsForBuyer = await wishListRepository.find({ where: { buyer: { id: req.user?.id } } }); - if (productsForBuyer.length === 0) { - return res.status(404).json({ message: 'No products in wish list' }); - } - - await wishListRepository.remove(productsForBuyer); - return res.status(200).json({ message: 'All products removed successfully'}); - - } catch (error) { - return res.status(500).json({ error: 'Internal server error' }); + if (productsForBuyer.length === 0) { + return res.status(404).json({ message: 'No products in wish list' }); } -} \ No newline at end of file + + await wishListRepository.remove(productsForBuyer); + return res.status(200).json({ message: 'All products removed successfully' }); + } catch (error) { + return res.status(500).json({ error: 'Internal server error' }); + } +}; diff --git a/src/services/wishListServices/getProducts.ts b/src/services/wishListServices/getProducts.ts index 107f3aa..98dc434 100644 --- a/src/services/wishListServices/getProducts.ts +++ b/src/services/wishListServices/getProducts.ts @@ -3,36 +3,37 @@ import { getRepository } from 'typeorm'; import { wishList } from '../../entities/wishList'; import { Product } from '../../entities/Product'; -export const getProductsService = async (req:Request,res:Response)=>{ - try { - const wishListRepository = getRepository(wishList); - const productRepository =getRepository(Product); +export const getProductsService = async (req: Request, res: Response) => { + try { + const wishListRepository = getRepository(wishList); + const productRepository = getRepository(Product); - const productsForBuyer = await wishListRepository.find({where: { buyer:{ id: req.user?.id} }}); + const productsForBuyer = await wishListRepository.find({ where: { buyer: { id: req.user?.id } } }); - if (productsForBuyer.length === 0) { - return res.status(404).json({ message: 'No products in wish list', products: productsForBuyer }); - } - - const buyerWishProducts = await Promise.all(productsForBuyer.map(async (product) => { - const productDetails = await productRepository.findOne({ where: { id: product.productId } }); - if(productDetails){ - return { - wishListDetails: product, - productInfo: { - productId: productDetails.id, - name: productDetails.name, - image: productDetails.images, - newPrice: productDetails.newPrice, - vendorId: productDetails.vendor - } - }; - } - })); + if (productsForBuyer.length === 0) { + return res.status(404).json({ message: 'No products in wish list', products: productsForBuyer }); + } - return res.status(200).json({ message: 'Products retrieved', productsForBuyer: buyerWishProducts }); + const buyerWishProducts = await Promise.all( + productsForBuyer.map(async product => { + const productDetails = await productRepository.findOne({ where: { id: product.productId } }); + if (productDetails) { + return { + wishListDetails: product, + productInfo: { + productId: productDetails.id, + name: productDetails.name, + image: productDetails.images, + newPrice: productDetails.newPrice, + vendorId: productDetails.vendor, + }, + }; + } + }) + ); - } catch (error) { - return res.status(500).json({ error: 'Internal server error' }); - } -} \ No newline at end of file + return res.status(200).json({ message: 'Products retrieved', productsForBuyer: buyerWishProducts }); + } catch (error) { + return res.status(500).json({ error: 'Internal server error' }); + } +}; diff --git a/src/services/wishListServices/removeProducts.ts b/src/services/wishListServices/removeProducts.ts index cb99c0f..b42052f 100644 --- a/src/services/wishListServices/removeProducts.ts +++ b/src/services/wishListServices/removeProducts.ts @@ -2,22 +2,20 @@ import { Request, Response } from 'express'; import { getRepository } from 'typeorm'; import { wishList } from '../../entities/wishList'; -export const removeProductService = async (req:Request,res:Response)=>{ - try { +export const removeProductService = async (req: Request, res: Response) => { + try { + const id = parseInt(req.params.id); + const wishListRepository = getRepository(wishList); - const id = parseInt(req.params.id); - const wishListRepository = getRepository(wishList); + const product = await wishListRepository.findOne({ where: { id } }); - const product = await wishListRepository.findOne({where: { id }}); - - if(!product){ - return res.status(404).json({message: "Product not found in wish list"}); - } - - await wishListRepository.remove(product); - return res.status(200).json({ message: "Product removed from wish list" }); - - } catch (error) { - return res.status(500).json({ error: 'Internal server error' }); + if (!product) { + return res.status(404).json({ message: 'Product not found in wish list' }); } -} \ No newline at end of file + + await wishListRepository.remove(product); + return res.status(200).json({ message: 'Product removed from wish list' }); + } catch (error) { + return res.status(500).json({ error: 'Internal server error' }); + } +}; diff --git a/src/startups/getSwaggerServer.ts b/src/startups/getSwaggerServer.ts index efe12fa..6a7416d 100644 --- a/src/startups/getSwaggerServer.ts +++ b/src/startups/getSwaggerServer.ts @@ -7,7 +7,7 @@ function getSwaggerServer (): string { return process.env.SWAGGER_SERVER; } - return `http://localhost:${process.env.PORT}/api/v1`; + return `http://localhost:${process.env.PORT}`; } export { getSwaggerServer }; diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 91874e3..623883f 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -1,72 +1,66 @@ /* eslint-disable camelcase */ import passport from 'passport'; -import { Strategy } from "passport-google-oauth20"; +import { Strategy } from 'passport-google-oauth20'; import { User } from '../entities/User'; import { getRepository } from 'typeorm'; import bcrypt from 'bcrypt'; -import "../utils/auth"; +import '../utils/auth'; passport.use( - new Strategy( - { - clientID: process.env.GOOGLE_CLIENT_ID as string, - clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, - callbackURL: 'http://localhost:6890/user/auth/google/callback/', - scope: ['email', 'profile'], - }, - async (accessToken: any, refreshToken: any, profile: any, cb: any) => { - const userRepository = getRepository(User); - const { family_name, - name, - picture, - email, - email_verified + new Strategy( + { + clientID: process.env.GOOGLE_CLIENT_ID as string, + clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, + callbackURL: 'http://localhost:6890/user/auth/google/callback/', + scope: ['email', 'profile'], + }, + async (accessToken: any, refreshToken: any, profile: any, cb: any) => { + const userRepository = getRepository(User); + const { family_name, name, picture, email, email_verified } = profile._json; + const { familyName, givenName } = profile.name; - } = profile._json; - const { familyName, givenName } = profile.name; + if (email || givenName || family_name || picture) { + try { + // Check for existing user + const existingUser = await userRepository.findOneBy({ email }); - if (email || givenName || family_name || picture) { - try { - // Check for existing user - const existingUser = await userRepository.findOneBy({ email }); + if (existingUser) { + return await cb(null, existingUser); + } + const saltRounds = 10; + const hashedPassword = await bcrypt.hash('password', saltRounds); + const newUser = new User(); + newUser.firstName = givenName; + newUser.lastName = family_name ?? familyName ?? 'undefined'; + newUser.email = email; + newUser.userType = 'Buyer'; + newUser.photoUrl = picture; + newUser.gender = 'Not specified'; + newUser.phoneNumber = 'Not specified'; + newUser.password = hashedPassword; + newUser.verified = email_verified; - if (existingUser) { - return await cb(null, existingUser); - } - const saltRounds = 10; - const hashedPassword = await bcrypt.hash("password", saltRounds); - const newUser = new User(); - newUser.firstName = givenName; - newUser.lastName = family_name ?? familyName ?? "undefined"; - newUser.email = email; - newUser.userType = 'Buyer'; - newUser.photoUrl = picture; - newUser.gender = "Not specified"; - newUser.phoneNumber = "Not specified"; - newUser.password = hashedPassword; - newUser.verified = email_verified; - - await userRepository.save(newUser); - return await cb(null, newUser); - } catch (error) { - console.error(error); - return await cb(error, null); - } - } - return await cb(null, profile, { message: 'Missing required profile information' }); + await userRepository.save(newUser); + return await cb(null, newUser); + } catch (error) { + console.error(error); + return await cb(error, null); } - ) + } + return await cb(null, profile, { message: 'Missing required profile information' }); + } + ) ); passport.serializeUser((user: any, cb) => { - cb(null, user.id); + cb(null, user.id); }); passport.deserializeUser(async (id: any, cb) => { - const userRepository = getRepository(User); - try { - const user = await userRepository.findOneBy({id}); - cb(null, user); - } catch (error) { - cb(error); - } + const userRepository = getRepository(User); + try { + const user = await userRepository.findOneBy({ id }); + cb(null, user); + } catch (error) { + cb(error); + } }); diff --git a/src/utils/index.ts b/src/utils/index.ts index 490375a..fdc7feb 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1 +1,20 @@ // export all utils +/** + * Format a number as a currency string. + * @param amount - The amount to format. + * @param currency - The currency code (e.g., 'USD', 'EUR'). Defaults to 'USD'. + * @returns The formatted currency string. + */ +export function formatMoney (amount: number, currency: string = 'RWF'): string { + return amount.toLocaleString('en-US', { style: 'currency', currency }); +} +/** + * Format a date string into a more readable format. + * @param dateString - The date string to format. + * @returns The formatted date string. + */ +export function formatDate (dateString: Date): string { + const options: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric' }; + const date = new Date(dateString); + return date.toLocaleDateString('en-US', options); +} diff --git a/src/utils/response.utils.ts b/src/utils/response.utils.ts index 3fd1ce5..f0e5513 100644 --- a/src/utils/response.utils.ts +++ b/src/utils/response.utils.ts @@ -45,3 +45,11 @@ export const responseServerError = (res: Response, error: string): Response { + return res.status(statusCode).json({ status: 'success', message, data }); +}; + +export const sendErrorResponse = (res: Response, statusCode: number, message: string) => { + return res.status(statusCode).json({ status: 'error', message }); +}; diff --git a/src/utils/sendOrderMail.ts b/src/utils/sendOrderMail.ts new file mode 100644 index 0000000..72ee5b0 --- /dev/null +++ b/src/utils/sendOrderMail.ts @@ -0,0 +1,215 @@ +import nodemailer from 'nodemailer'; +import { formatMoney, formatDate } from './index'; + +interface Product { + name: string; + newPrice: number; + quantity: number; +} + +interface Message { + subject: string; + fullName: string; + email: string; + products: Product[]; + totalAmount: number; + quantity: number; + orderDate: Date; + address: string; +} + +const sendMail = async (message: Message) => { + const transporter = nodemailer.createTransport({ + host: process.env.HOST, + port: 587, + secure: false, // true for 465, false for other ports + auth: { + user: process.env.AUTH_EMAIL as string, + pass: process.env.AUTH_PASSWORD as string, + }, + }); + + const { subject, fullName, email, products, totalAmount, quantity, orderDate, address } = message; + + const mailOptions = { + to: email, + subject: subject, + html: ` + + + + + + + Order Details + + + + +
+ shoping image +

Order Success

+
+

User information

+ + + + + + + + + + + + + + + +
Full Name:Email:Address:
${fullName}${email}${address}
+ + + + + + + + + + + + + + + +
Order Date:Quantity:Total Amount:
${formatDate(orderDate)}${quantity}${formatMoney(totalAmount)}
+
+
+

Products

+ + + + + + + + ${products + .map( + (product: Product) => ` + + + + + + + ` + ) + .join('')} + + + + +
Product NameProduct PriceQuantityTotal
${product.name}${formatMoney(product.newPrice)}${product.quantity}${product.quantity * product.newPrice}
Total${totalAmount}
+
+ +
+ + + `, + }; + + try { + const info = await transporter.sendMail(mailOptions); + console.log('Email sent: ' + info.response); + } catch (error) { + console.log('Error occurred while sending email', error); + } +}; + +export default sendMail; diff --git a/src/utils/sendOrderMailUpdated.ts b/src/utils/sendOrderMailUpdated.ts new file mode 100644 index 0000000..adddc9a --- /dev/null +++ b/src/utils/sendOrderMailUpdated.ts @@ -0,0 +1,215 @@ +import nodemailer from 'nodemailer'; +import { formatMoney, formatDate } from './index'; + +interface Product { + name: string; + newPrice: number; + quantity: number; +} + +interface Message { + subject: string; + fullName: string; + email: string; + products: Product[]; + totalAmount: number; + quantity: number; + orderDate: Date; + address: string; +} + +const sendMail = async (message: Message) => { + const transporter = nodemailer.createTransport({ + host: process.env.HOST, + port: 587, + secure: false, // true for 465, false for other ports + auth: { + user: process.env.AUTH_EMAIL as string, + pass: process.env.AUTH_PASSWORD as string, + }, + }); + + const { subject, fullName, email, products, totalAmount, quantity, orderDate, address } = message; + + const mailOptions = { + to: email, + subject: subject, + html: ` + + + + + + + Your order details have been updated + + + + +
+ shoping image +

Order Updated

+
+

User information

+ + + + + + + + + + + + + + + +
Full Name:Email:Address:
${fullName}${email}${address}
+ + + + + + + + + + + + + + + +
Order Date:Quantity:Total Amount:
${formatDate(orderDate)}${quantity}${formatMoney(totalAmount)}
+
+
+

Products

+ + + + + + + + ${products + .map( + (product: Product) => ` + + + + + + + ` + ) + .join('')} + + + + +
Product NameProduct PriceQuantityTotal
${product.name}${formatMoney(product.newPrice)}${product.quantity}${product.quantity * product.newPrice}
Total${totalAmount}
+
+ +
+ + + `, + }; + + try { + const info = await transporter.sendMail(mailOptions); + console.log('Email sent: ' + info.response); + } catch (error) { + console.log('Error occurred while sending email', error); + } +}; + +export default sendMail; diff --git a/src/utils/socket.ts b/src/utils/socket.ts new file mode 100644 index 0000000..e32ae19 --- /dev/null +++ b/src/utils/socket.ts @@ -0,0 +1,21 @@ +import { Server as HTTPServer } from 'http'; +import { Server as SocketIOServer } from 'socket.io'; + +let io: SocketIOServer | undefined; + +export const init = (httpServer: HTTPServer): SocketIOServer => { + io = new SocketIOServer(httpServer, { + cors: { + origin: '*', + methods: ['GET', 'POST', 'DELETE', 'PUT'], + }, + }); + return io; +}; + +export const getIO = (): SocketIOServer => { + if (!io) { + throw new Error('Socket.io not initialized!'); + } + return io; +}; diff --git a/tsconfig.json b/tsconfig.json index 4665a6d..d58c75f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,108 +1,108 @@ { - "compilerOptions": { - /* Visit https://aka.ms/tsconfig to read more about this file */ - /* Projects */ - // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ - // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ - // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ - // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ - // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ - // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ - /* Language and Environment */ - "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, - // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ - // "jsx": "preserve", /* Specify what JSX code is generated. */ - "experimentalDecorators": true /* Enable experimental support for legacy experimental decorators. */, - "emitDecoratorMetadata": true /* Emit design-type metadata for decorated declarations in source files. */, - // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ - // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ - // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ - // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ - // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ - // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ - // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ - /* Modules */ - "module": "commonjs" /* Specify what module code is generated. */, - "rootDir": "./src" /* Specify the root folder within your source files. */, - // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ - // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ - // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ - // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ - // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ - "types": [ - "node", - "jest", - "express", - "joi" - ] /* Specify type package names to be included without being referenced in a source file. */, - // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ - // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ - // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ - // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ - // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ - // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ - // "resolveJsonModule": true, /* Enable importing .json files. */ - // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ - // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ - /* JavaScript Support */ - "allowJs": true /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */, - // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ - // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ - /* Emit */ - // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ - // "declarationMap": true, /* Create sourcemaps for d.ts files. */ - // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ - // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ - // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ - // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ - "outDir": "./dist" /* Specify an output folder for all emitted files. */, - // "removeComments": true, /* Disable emitting comments. */ - // "noEmit": true, /* Disable emitting files from a compilation. */ - // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ - // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ - // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ - // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ - // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ - // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ - // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ - // "newLine": "crlf", /* Set the newline character for emitting files. */ - // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ - // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ - // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ - // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ - // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ - /* Interop Constraints */ - // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ - // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ - // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ - "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, - // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ - "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, - /* Type Checking */ - "strict": true /* Enable all strict type-checking options. */, - // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ - // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ - // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ - // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ - // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ - // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ - // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ - // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ - // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ - // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ - // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ - // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ - // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ - // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ - // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ - // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ - // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ - // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ - /* Completeness */ - // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ - "skipLibCheck": true /* Skip type checking all .d.ts files. */ - }, - "include": ["src/**/*.ts"], - "exclude": ["node_modules"] - } \ No newline at end of file + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + /* Projects */ + // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + /* Language and Environment */ + "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + "experimentalDecorators": true /* Enable experimental support for legacy experimental decorators. */, + "emitDecoratorMetadata": true /* Emit design-type metadata for decorated declarations in source files. */, + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ + /* Modules */ + "module": "commonjs" /* Specify what module code is generated. */, + "rootDir": "./src" /* Specify the root folder within your source files. */, + // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ + "types": [ + "node", + "jest", + "express", + "joi" + ] /* Specify type package names to be included without being referenced in a source file. */, + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ + // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ + // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ + // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ + // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ + /* JavaScript Support */ + "allowJs": true /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */, + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ + /* Emit */ + // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./dist" /* Specify an output folder for all emitted files. */, + // "removeComments": true, /* Disable emitting comments. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, + /* Type Checking */ + "strict": true /* Enable all strict type-checking options. */, + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + }, + "include": ["src/**/*.ts"], + "exclude": ["node_modules"] +} From f976460b3d83f4316ba66c9ef5ccf2ca9e25fb1f Mon Sep 17 00:00:00 2001 From: Iadivin Date: Fri, 31 May 2024 07:53:48 +0200 Subject: [PATCH 3/4] feat(docker): implement Docker for project - Include Docker file to build image for project and containerize the project - Configure Docker compose for easy setup --- .dockerignore | 5 +++++ Dockerfile | 13 +++++++++++++ README.md | 18 ++++++++++++++++++ docker-compose.yml | 27 +++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..2deef83 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +node_modules +npm-debug.log +Dockerfile +docker-compose.yml +.dockerignore \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5190e01 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM node:20-alpine + +WORKDIR /app + +COPY package.json . + +RUN npm install + +COPY . . + +EXPOSE $PORT + +CMD ["npm", "run", "dev"] diff --git a/README.md b/README.md index 0d6c5f0..453c9d5 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,24 @@ logger.debug('This is a debug message'); npm test ``` +### Setting up docker and using it + +- Download and install docker + ``` + https://www.docker.com/products/docker-desktop/ + ``` +- Download Subsystem for Linux for none linux users +- Set environment varibles like database host to postgresdb + +- Building the image, you must navigate to the project directory in the terminal, then run + ``` + docker-compose up --build + ``` +- Stoping docker-compose container, run + ``` + docker-compose down + ``` + ## Authors - [Maxime Mizero](https://github.com/maxCastro1) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..99ed647 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,27 @@ +version: '3.8' + +services: + postgresdb: + image: postgres + environment: + POSTGRES_USER: $DEV_DB_USER + POSTGRES_PASSWORD: $DEV_DB_PASS + POSTGRES_DB: $DEV_DB_NAME + volumes: + - knights-data:/var/lib/postgresql/data + + node-app: + build: . + volumes: + - .:/app + - /app/node_modules + image: knights-app:1.0 + env_file: + - ./.env + ports: + - $PORT:$PORT + depends_on: + - postgresdb + +volumes: + knights-data: From c5aa42c180c9bd5e7f99e055d60f99f374cfbf5d Mon Sep 17 00:00:00 2001 From: Icyeza Date: Thu, 30 May 2024 13:15:07 +0200 Subject: [PATCH 4/4] Implement buyer able to leave feedback on products ft adding discount coupon feature implementing order tracking and order managment issue implementing order tracking and order managment issue implementing order tracking and order managment issue implementing order tracking and order managment issue adding documentation and tests writing tests Feat-Buyer-coupon-discount-management implementing order tracking and order managment issue implementing order tracking and order managment issue implementing order tracking and order managment issue implementing order tracking and order managment issue adding documentation and tests writing tests implementing stripe payment system implementing stripe payment system Implement buyer able to leave feedback on products Implement buyer able to leave feedback on products --- src/__test__/cart.test.ts | 219 ++++++++++-------- src/__test__/test-assets/DatabaseCleanup.ts | 2 + src/controllers/feedbackController.ts | 21 ++ src/entities/Feedback.ts | 30 +++ src/entities/Order.ts | 5 + src/entities/Product.ts | 5 +- src/entities/User.ts | 3 + src/routes/ProductRoutes.ts | 1 - src/routes/feedbackRoutes.ts | 19 ++ src/routes/index.ts | 2 + .../feedbackServices/adminDeleteFeedback.ts | 25 ++ .../feedbackServices/createFeedback.ts | 44 ++++ .../feedbackServices/deleteFeedback.ts | 27 +++ .../feedbackServices/updateFeedback.ts | 32 +++ src/services/orderServices/createOrder.ts | 14 +- .../getOrderTransactionHistory.ts | 2 - .../orderServices/updateOrderService.ts | 5 - .../getRecommendedProductsService.ts | 1 + .../productServices/listAllProductsService.ts | 2 +- src/services/productServices/readProduct.ts | 2 +- .../productServices/viewSingleProduct.ts | 2 +- 21 files changed, 338 insertions(+), 125 deletions(-) create mode 100644 src/controllers/feedbackController.ts create mode 100644 src/entities/Feedback.ts create mode 100644 src/routes/feedbackRoutes.ts create mode 100644 src/services/feedbackServices/adminDeleteFeedback.ts create mode 100644 src/services/feedbackServices/createFeedback.ts create mode 100644 src/services/feedbackServices/deleteFeedback.ts create mode 100644 src/services/feedbackServices/updateFeedback.ts diff --git a/src/__test__/cart.test.ts b/src/__test__/cart.test.ts index 4d6d1f0..7fe7146 100644 --- a/src/__test__/cart.test.ts +++ b/src/__test__/cart.test.ts @@ -14,6 +14,7 @@ import { cleanDatabase } from './test-assets/DatabaseCleanup'; const vendor1Id = uuid(); const buyer1Id = uuid(); const buyer2Id = uuid(); +const buyer3Id = uuid(); const product1Id = uuid(); const product2Id = uuid(); const catId = uuid(); @@ -52,7 +53,7 @@ const sampleBuyer1: UserInterface = { id: buyer1Id, firstName: 'buyer1', lastName: 'user', - email: 'elijahladdiedv@gmail.com', + email: 'manger@gmail.com', password: 'password', userType: 'Buyer', gender: 'Male', @@ -65,7 +66,7 @@ const sampleBuyer2: UserInterface = { id: buyer2Id, firstName: 'buyer1', lastName: 'user', - email: 'buyer1112@example.com', + email: 'elijahladdiedv@example.com', password: 'password', userType: 'Buyer', gender: 'Male', @@ -73,6 +74,18 @@ const sampleBuyer2: UserInterface = { photoUrl: 'https://example.com/photo.jpg', role: 'BUYER', }; +const sampleBuyer3: UserInterface = { + id: buyer3Id, + firstName: 'buyer1', + lastName: 'user', + email: 'elhladdiedv@example.com', + password: 'password', + userType: 'Admin', + gender: 'Male', + phoneNumber: '121163800', + photoUrl: 'https://example.com/photo.jpg', + role: 'ADMIN', +}; const sampleCat = { id: catId, @@ -175,7 +188,7 @@ afterAll(async () => { server.close(); }); -describe('Cart management for guest/buyer', () => { +describe('Cart| Order management for guest/buyer', () => { describe('Creating new product', () => { it('should create new product', async () => { const response = await request(app) @@ -376,6 +389,108 @@ describe('Cart management for guest/buyer', () => { }); }); + describe('Order management tests', () => { + let orderId: any; + let productId: any; + let feedbackId: any; + let feedback2Id: any; + describe('Create order', () => { + it('should return 400 when user ID is not provided', async () => { + const response = await request(app) + .post('/product/orders') + .send({ + address: { + country: 'Test Country', + city: 'Test City', + street: 'Test Street', + }, + }) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(201); + }); + + it('should return orders for the buyer', async () => { + const response = await request(app) + .get('/product/client/orders') + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(200); + orderId = response.body.data.orders[0]?.id; + productId = response.body.data.orders[0]?.orderItems[0]?.product?.id; + }); + it('should return 404 if the buyer has no orders', async () => { + const response = await request(app) + .get('/product/client/orders') + .set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`); + expect(response.status).toBe(404); + expect(response.body.message).toBeUndefined; + }); + + it('should return transaction history for the buyer', async () => { + const response = await request(app) + .get('/product/orders/history') + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(200); + expect(response.body.message).toBe('Transaction history retrieved successfully'); + }); + + it('should return 400 when user ID is not provided', async () => { + const response = await request(app) + .get('/product/orders/history') + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(200); + }); + }); + + describe('Update order', () => { + it('should update order status successfully', async () => { + const response = await request(app) + .put(`/product/client/orders/${orderId}`) + .send({ orderStatus: 'completed' }) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(200); + }); + }); + describe('Add feedback to the product with order', () => { + it('should create new feedback to the ordered product', async () => { + const response = await request(app) + .post(`/feedback/${productId}/new`) + .send({ orderId, comment: 'Well this product looks so fantastic' }) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(201); + feedbackId = response.body.data.id + }); + it('should create new feedback to the ordered product', async () => { + const response = await request(app) + .post(`/feedback/${productId}/new`) + .send({ orderId, comment: 'Murigalike this product looks so fantastic' }) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(201); + feedback2Id = response.body.data.id + }); + it('should updated existing feedback successfully', async () => { + const response = await request(app) + .put(`/feedback/update/${feedbackId}`,) + .send({ orderId, comment: 'Well this product looks so lovely' }) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(200); + }); + it('should remove recorded feedback', async () => { + const response = await request(app) + .delete(`/feedback/delete/${feedbackId}`) + .send({ orderId, comment: 'Well this product looks so lovely' }) + .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); + expect(response.status).toBe(200); + }); + it('should remove recorder feedback as admin ', async () => { + const response = await request(app) + .delete(`/feedback/admin/delete/${feedback2Id}`) + .send({ orderId, comment: 'Well this product looks so lovely' }) + .set('Authorization', `Bearer ${getAccessToken(buyer3Id, sampleBuyer3.email)}`); + expect(response.status).toBe(401); + }); + }); + }); + describe('Deleting product from cart', () => { it('should return 404 if product does not exist in cart', async () => { const response = await request(app) @@ -511,101 +626,3 @@ describe('Cart management for guest/buyer', () => { }); }); }); - -describe('Order management tests', () => { - let orderId: string | null; - describe('Create order', () => { - it('should return 400 when user ID is not provided', async () => { - const response = await request(app) - .post('/product/orders') - .send({ - address: { - country: 'Test Country', - city: 'Test City', - street: 'Test Street', - }, - }) - .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); - expect(response.status).toBe(400); - }); - - it('should create a new order', async () => { - const response = await request(app) - .post('/product/orders') - .send({ - address: { - country: 'Test Country', - city: 'Test City', - street: 'Test Street', - }, - }) - .set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`); - - expect(response.status).toBe(400); - expect(response.body.message).toBeUndefined; - orderId = response.body.data?.orderId; // Assuming orderId is returned in response - }); - - it('should insert a new order', async () => { - const response = await request(app) - .post('/product/orders') - .send({ - address: { - country: 'Test Country', - city: 'Test City', - street: 'Test Street', - }, - }) - .set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`); - - expect(response.status).toBe(400); - expect(response.body.message).toBeUndefined; - orderId = response.body.data?.orderId; // Assuming orderId is returned in response - }); - }); - - describe('Get orders', () => { - it('should return orders for the buyer', async () => { - const response = await request(app) - .get('/product/client/orders') - .set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`); - expect(response.status).toBe(404); - expect(response.body.message).toBeUndefined; - }); - - it('should return 404 if the buyer has no orders', async () => { - const response = await request(app) - .get('/product/client/orders') - .set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`); - expect(response.status).toBe(404); - expect(response.body.message).toBeUndefined; - }); - }); - - describe('Get transaction history', () => { - it('should return transaction history for the buyer', async () => { - const response = await request(app) - .get('/product/orders/history') - .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); - expect(response.status).toBe(404); - expect(response.body.message).toBe('No transaction history found'); - }); - - it('should return 400 when user ID is not provided', async () => { - const response = await request(app) - .get('/product/orders/history') - .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); - expect(response.status).toBe(404); - }); - }); - - describe('Update order', () => { - it('should update order status successfully', async () => { - const response = await request(app) - .put(`/product/client/orders/${orderId}`) - .send({ orderStatus: 'delivered' }) - .set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`); - expect(response.status).toBe(500); - }); - }); -}); diff --git a/src/__test__/test-assets/DatabaseCleanup.ts b/src/__test__/test-assets/DatabaseCleanup.ts index 3674dfb..b8739d1 100644 --- a/src/__test__/test-assets/DatabaseCleanup.ts +++ b/src/__test__/test-assets/DatabaseCleanup.ts @@ -12,11 +12,13 @@ import { User } from '../../entities/User'; import { server } from '../..'; import { VendorOrderItem } from '../../entities/VendorOrderItem'; import { VendorOrders } from '../../entities/vendorOrders'; +import { Feedback } from '../../entities/Feedback'; export const cleanDatabase = async () => { const connection = getConnection(); // Delete from child tables first + await connection.getRepository(Feedback).delete({}); await connection.getRepository(Transaction).delete({}); await connection.getRepository(Coupon).delete({}); await connection.getRepository(VendorOrderItem).delete({}); diff --git a/src/controllers/feedbackController.ts b/src/controllers/feedbackController.ts new file mode 100644 index 0000000..0cbce14 --- /dev/null +++ b/src/controllers/feedbackController.ts @@ -0,0 +1,21 @@ +import { Request, Response } from 'express'; +import { createFeedbackService } from '../services/feedbackServices/createFeedback'; +import { updateFeedbackService } from '../services/feedbackServices/updateFeedback'; +import { deleteFeedbackService } from '../services/feedbackServices/deleteFeedback'; +import { adminDeleteFeedbackService } from '../services/feedbackServices/adminDeleteFeedback'; + +export const createFeedback = async (req: Request, res: Response) => { + await createFeedbackService(req, res); +}; + +export const updateFeedback = async (req: Request, res: Response) => { + await updateFeedbackService(req, res); +}; + +export const deleteFeedback = async (req: Request, res: Response) => { + await deleteFeedbackService(req, res); +}; + +export const adminDeleteFeedback = async (req: Request, res: Response) => { + await adminDeleteFeedbackService(req, res); +}; diff --git a/src/entities/Feedback.ts b/src/entities/Feedback.ts new file mode 100644 index 0000000..6de9058 --- /dev/null +++ b/src/entities/Feedback.ts @@ -0,0 +1,30 @@ +import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, CreateDateColumn, UpdateDateColumn } from 'typeorm'; +import { User } from './User'; +import { Product } from './Product'; +import { IsNotEmpty } from 'class-validator'; +import { Order } from './Order'; + +@Entity() +export class Feedback { + @PrimaryGeneratedColumn('uuid') + @IsNotEmpty() + id!: string; + + @Column('text') + comment!: string; + + @ManyToOne(() => User, user => user.feedbacks) + user!: User; + + @ManyToOne(() => Product, product => product.feedbacks) + product!: Product; + + @ManyToOne(() => Order, order => order.feedbacks) + order!: Order; + + @CreateDateColumn() + createdAt!: Date; + + @UpdateDateColumn() + updatedAt!: Date; +} diff --git a/src/entities/Order.ts b/src/entities/Order.ts index 47649a7..faa19db 100644 --- a/src/entities/Order.ts +++ b/src/entities/Order.ts @@ -11,6 +11,7 @@ import { IsNotEmpty, IsNumber, IsDate, IsIn } from 'class-validator'; import { User } from './User'; import { OrderItem } from './OrderItem'; import { Transaction } from './transaction'; +import { Feedback } from './Feedback'; @Entity() export class Order { @@ -33,6 +34,10 @@ export class Order { @OneToMany(() => Transaction, transaction => transaction.order) transactions!: Transaction[]; + + @OneToMany(() => Feedback, feedback => feedback.order) + feedbacks!: Feedback[]; + @Column({ default: 'order placed' }) @IsNotEmpty() @IsIn([ diff --git a/src/entities/Product.ts b/src/entities/Product.ts index e144a04..ae027ef 100644 --- a/src/entities/Product.ts +++ b/src/entities/Product.ts @@ -19,11 +19,12 @@ import { Order } from './Order'; import { Coupon } from './coupon'; import { OrderItem } from './OrderItem'; import { VendorOrderItem } from './VendorOrderItem'; +import { Feedback } from './Feedback'; @Entity() @Unique(['id']) export class Product { - static query () { + static query() { throw new Error('Method not implemented.'); } @PrimaryGeneratedColumn('uuid') @@ -39,6 +40,8 @@ export class Product { @OneToMany(() => VendorOrderItem, vendorOrderItems => vendorOrderItems.product) vendorOrderItems!: VendorOrderItem[]; + @OneToMany(() => Feedback, feedback => feedback.product) + feedbacks!: Feedback[]; @OneToOne(() => Coupon, (coupons: any) => coupons.product) @JoinColumn() diff --git a/src/entities/User.ts b/src/entities/User.ts index ef256d7..232ce11 100644 --- a/src/entities/User.ts +++ b/src/entities/User.ts @@ -13,6 +13,7 @@ import { IsEmail, IsNotEmpty, IsString, IsBoolean, IsIn } from 'class-validator' import { roles } from '../utils/roles'; import { Order } from './Order'; import { Transaction } from './transaction'; +import { Feedback } from './Feedback'; export interface UserInterface { id?: string; @@ -111,6 +112,8 @@ export class User { @Column({ type: 'numeric', precision: 24, scale: 2, default: 0 }) accountBalance!: number; + @OneToMany(() => Feedback, feedback => feedback.product) + feedbacks!: Feedback[]; @BeforeInsert() setRole (): void { diff --git a/src/routes/ProductRoutes.ts b/src/routes/ProductRoutes.ts index 4a72b47..2352000 100644 --- a/src/routes/ProductRoutes.ts +++ b/src/routes/ProductRoutes.ts @@ -26,7 +26,6 @@ import { getSingleBuyerVendorOrder, updateBuyerVendorOrder, } from '../controllers'; - const router = Router(); router.get('/all', listAllProducts); router.get('/recommended', authMiddleware as RequestHandler, hasRole('BUYER'), getRecommendedProducts); diff --git a/src/routes/feedbackRoutes.ts b/src/routes/feedbackRoutes.ts new file mode 100644 index 0000000..3ada81b --- /dev/null +++ b/src/routes/feedbackRoutes.ts @@ -0,0 +1,19 @@ +import { RequestHandler, Router } from 'express'; +import { + createFeedback, + updateFeedback, + deleteFeedback, + adminDeleteFeedback +} from '../controllers/feedbackController' +import { authMiddleware } from '../middlewares/verifyToken'; +import { hasRole } from '../middlewares/roleCheck'; + + +const router = Router(); + +router.post('/:productId/new', authMiddleware as RequestHandler, hasRole('BUYER'), createFeedback); +router.put('/update/:feedbackId', authMiddleware as RequestHandler, hasRole('BUYER'), updateFeedback ); +router.delete('/delete/:feedbackId', authMiddleware as RequestHandler, hasRole('BUYER'), deleteFeedback); +router.delete('/admin/delete/:feedbackId', authMiddleware as RequestHandler, hasRole('ADMIN'), adminDeleteFeedback ); + +export default router; diff --git a/src/routes/index.ts b/src/routes/index.ts index 6f632d6..ee8dcdb 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -5,6 +5,7 @@ import productRoutes from './ProductRoutes'; import wishListRoutes from './wishListRoute'; import couponRoute from './couponRoutes'; import cartRoutes from './CartRoutes'; +import feedbackRoute from './feedbackRoutes'; const router = Router(); @@ -17,5 +18,6 @@ router.use('/product', productRoutes); router.use('/wish-list', wishListRoutes); router.use('/cart', cartRoutes); router.use('/coupons', couponRoute); +router.use('/feedback', feedbackRoute); export default router; diff --git a/src/services/feedbackServices/adminDeleteFeedback.ts b/src/services/feedbackServices/adminDeleteFeedback.ts new file mode 100644 index 0000000..7bf6261 --- /dev/null +++ b/src/services/feedbackServices/adminDeleteFeedback.ts @@ -0,0 +1,25 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { Feedback } from '../../entities/Feedback'; +import { responseError, responseSuccess } from '../../utils/response.utils'; + +export const adminDeleteFeedbackService = async (req: Request, res: Response) => { + const { feedbackId } = req.params; + + try { + const feedbackRepository = getRepository(Feedback); + const feedback = await feedbackRepository.findOne({ + where: { id: feedbackId }, + }); + + if (!feedback) { + return responseError(res, 404, 'Feedback not found'); + } + + await feedbackRepository.remove(feedback); + + return responseSuccess(res, 200, 'Feedback successfully removed'); + } catch (error) { + return responseError(res, 500, 'Server error'); + } +}; diff --git a/src/services/feedbackServices/createFeedback.ts b/src/services/feedbackServices/createFeedback.ts new file mode 100644 index 0000000..fa731f3 --- /dev/null +++ b/src/services/feedbackServices/createFeedback.ts @@ -0,0 +1,44 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { Feedback } from '../../entities/Feedback'; +import { Product } from '../../entities/Product'; +import { User } from '../../entities/User'; +import { responseError, responseSuccess } from '../../utils/response.utils'; +import { Order } from '../../entities/Order'; + +interface AuthRequest extends Request { + user?: User; +} + +export const createFeedbackService = async (req: Request, res: Response) => { + const { productId } = req.params; + const { comment, orderId } = req.body; + + try { + const feedbackRepository = getRepository(Feedback); + const productRepository = getRepository(Product); + const orderRepository = getRepository(Order); + if (!orderId) { + return responseError(res, 404, `Your feedback can't be recorded at this time Your order doesn't exist `); + } + const product = await productRepository.findOne({ where: { id: productId } }); + if (!product) { + return responseError(res, 404, `Your feedback can't be recorded at this time product not found`); + } + const order = await orderRepository.findBy({ id: orderId, orderStatus: 'completed', buyer: { id: req.user?.id }, orderItems: { product: { id: productId } } }) + if (!order.length) { + return responseError(res, 404, `Your feedback can't be recorded at this time Your order haven't been completed yet or doesn't contain this product`); + } + + const feedback = new Feedback(); + feedback.comment = comment; + feedback.user = req.user as User; + feedback.product = product; + + await feedbackRepository.save(feedback); + + return responseSuccess(res, 201, 'Feedback created successfully', feedback); + } catch (error) { + return responseError(res, 500, 'Server error'); + } +}; diff --git a/src/services/feedbackServices/deleteFeedback.ts b/src/services/feedbackServices/deleteFeedback.ts new file mode 100644 index 0000000..5de4ea0 --- /dev/null +++ b/src/services/feedbackServices/deleteFeedback.ts @@ -0,0 +1,27 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { Feedback } from '../../entities/Feedback'; +import { responseError, responseSuccess } from '../../utils/response.utils'; + +export const deleteFeedbackService = async (req: Request, res: Response) => { + const { feedbackId } = req.params; + + try { + const feedbackRepository = getRepository(Feedback); + const feedback = await feedbackRepository.findOne({ + where: { id: feedbackId, + user: {id: req?.user?.id }, + } + }); + + if (!feedback) { + return responseError(res, 404, 'Feedback not found'); + } + + await feedbackRepository.remove(feedback); + + return responseSuccess(res, 200, 'Feedback successfully removed'); + } catch (error) { + return responseError(res, 500, 'Server error'); + } +}; diff --git a/src/services/feedbackServices/updateFeedback.ts b/src/services/feedbackServices/updateFeedback.ts new file mode 100644 index 0000000..18258c2 --- /dev/null +++ b/src/services/feedbackServices/updateFeedback.ts @@ -0,0 +1,32 @@ +import { Request, Response } from 'express'; +import { getRepository } from 'typeorm'; +import { Feedback } from '../../entities/Feedback'; +import { responseError, responseSuccess } from '../../utils/response.utils'; +import { User } from '../../entities/User'; + +export const updateFeedbackService = async (req: Request, res: Response) => { + const { feedbackId } = req.params; + const { comment } = req.body; + + try { + const feedbackRepository = getRepository(Feedback); + + const feedback = await feedbackRepository.findOne({ + where: { + id: feedbackId, + user: { id: req?.user?.id }, + }, + }); + + if (!feedback) { + return responseError(res, 404, 'You are not allowed to remove this feedback or you are not allowed to edit this feedback'); + } + + feedback.comment = comment; + await feedbackRepository.save(feedback); + + return responseSuccess(res, 200, 'Feedback updated successfully', feedback); + } catch (error) { + return responseError(res, 500, 'Server error'); + } +}; diff --git a/src/services/orderServices/createOrder.ts b/src/services/orderServices/createOrder.ts index 256bf40..7e1916e 100644 --- a/src/services/orderServices/createOrder.ts +++ b/src/services/orderServices/createOrder.ts @@ -59,15 +59,6 @@ export const createOrderService = async (req: Request, res: Response) => { orderItem.quantity = item.quantity; 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; @@ -94,8 +85,6 @@ 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); @@ -105,6 +94,7 @@ export const createOrderService = async (req: Request, res: Response) => { }); const orderResponse = { + id: newOrder.id, fullName: `${newOrder.buyer.firstName} ${newOrder.buyer.lastName}`, email: newOrder.buyer.email, products: orderItems.map(item => ({ @@ -174,7 +164,7 @@ const saveVendorRelatedOrder = async (order: Order, CartItem: CartItem[]) => { newVendorOrders.vendor = product.vendor; newVendorOrders.vendorOrderItems = [orderItem]; newVendorOrders.order = order; - newVendorOrders.totalPrice = +product.newPrice * item.quantity; + newVendorOrders.totalPrice = product.newPrice * item.quantity; vendorOrders = newVendorOrders; } diff --git a/src/services/orderServices/getOrderTransactionHistory.ts b/src/services/orderServices/getOrderTransactionHistory.ts index 74ae473..6bd0b17 100644 --- a/src/services/orderServices/getOrderTransactionHistory.ts +++ b/src/services/orderServices/getOrderTransactionHistory.ts @@ -23,8 +23,6 @@ export const getTransactionHistoryService = async (req: Request, res: Response) id: transaction.id, amount: transaction.amount, type: transaction.type, - previousBalance: transaction.previousBalance, - currentBalance: transaction.currentBalance, description: transaction.description, createdAt: transaction.createdAt, order: transaction.order diff --git a/src/services/orderServices/updateOrderService.ts b/src/services/orderServices/updateOrderService.ts index 6a163fe..bfddf1f 100644 --- a/src/services/orderServices/updateOrderService.ts +++ b/src/services/orderServices/updateOrderService.ts @@ -102,9 +102,6 @@ async function processRefund (order: Order, entityManager: EntityManager) { const buyer = order.buyer; // Refund buyer - const previousBalance = buyer.accountBalance; - buyer.accountBalance += order.totalPrice; - const currentBalance = buyer.accountBalance; await entityManager.save(buyer); // Record refund transaction @@ -112,8 +109,6 @@ async function processRefund (order: Order, entityManager: EntityManager) { refundTransaction.user = buyer; refundTransaction.order = order; refundTransaction.amount = order.totalPrice; - refundTransaction.previousBalance = previousBalance; - refundTransaction.currentBalance = currentBalance; refundTransaction.type = 'credit'; refundTransaction.description = 'Refund for cancelled or returned order'; await entityManager.save(refundTransaction); diff --git a/src/services/productServices/getRecommendedProductsService.ts b/src/services/productServices/getRecommendedProductsService.ts index fde015d..533dcd9 100644 --- a/src/services/productServices/getRecommendedProductsService.ts +++ b/src/services/productServices/getRecommendedProductsService.ts @@ -30,6 +30,7 @@ export const getRecommendedProductsService = async (req: Request, res: Response) .createQueryBuilder('product') .leftJoinAndSelect('product.categories', 'category') .leftJoinAndSelect('product.vendor', 'vendor') + .leftJoinAndSelect('product.feedbacks', 'feedbacks') .where('1 = 1'); if (condition.categories && condition.categories.length > 0) { diff --git a/src/services/productServices/listAllProductsService.ts b/src/services/productServices/listAllProductsService.ts index f39c7bb..4429e89 100644 --- a/src/services/productServices/listAllProductsService.ts +++ b/src/services/productServices/listAllProductsService.ts @@ -20,7 +20,7 @@ export const listAllProductsService = async (req: Request, res: Response) => { }, skip, take: limit, - relations: ['categories', 'vendor'], + relations: ['categories', 'vendor', 'feedbacks'], select: { vendor: { id: true, diff --git a/src/services/productServices/readProduct.ts b/src/services/productServices/readProduct.ts index b3c244d..2836b21 100644 --- a/src/services/productServices/readProduct.ts +++ b/src/services/productServices/readProduct.ts @@ -20,7 +20,7 @@ export const readProductsService = async (req: Request, res: Response) => { }, skip, take: limit, - relations: ['categories', 'vendor'], + relations: ['categories', 'vendor', 'feedbacks'], select: { vendor: { id: true, diff --git a/src/services/productServices/viewSingleProduct.ts b/src/services/productServices/viewSingleProduct.ts index 29ac167..be9764d 100644 --- a/src/services/productServices/viewSingleProduct.ts +++ b/src/services/productServices/viewSingleProduct.ts @@ -13,7 +13,7 @@ export const viewSingleProduct = async (req: Request, res: Response) => { } if (productId) { const products = getRepository(Product); - const product = await products.findOneBy({ id: productId }); + const product = await products.findOne({ where: { id: productId }, relations: ['categories', 'vendor', 'feedbacks'], }); if (!product) { return res.status(404).send({ status: 'error', message: 'Product not found' });