Skip to content

Commit

Permalink
Add missing endpoints for admin and seller dashboards (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jadowacu1 authored Aug 4, 2024
1 parent b3948ca commit b5d7201
Show file tree
Hide file tree
Showing 15 changed files with 421 additions and 15 deletions.
158 changes: 156 additions & 2 deletions src/databases/seeders/20240604133044-orders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable comma-dangle */
import { QueryInterface } from "sequelize";
import { cartOneId, orderOneId, orderTwoId, shopOneId,productTwoId,productOneId } from "../../types/uuid";
import { cartOneId, orderOneId, orderTwoId, shopOneId,productTwoId,productOneId, orderThreeId, orderFourId, orderFiveId, orderSixId, orderSevenId, orderEightId, orderNineId, shopTwoId } from "../../types/uuid";

module.exports = {
async up(queryInterface: QueryInterface) {
Expand Down Expand Up @@ -48,7 +48,161 @@ module.exports = {
expectedDeliveryDate: new Date("2024-08-05"),
createdAt: new Date(),
updatedAt: new Date()
}
},
{
id: orderThreeId,
products:JSON.stringify( [
{
productId:productOneId,
status:"pending"
},
{
productId:productTwoId,
status:"pending"
}
]),
shopId: shopOneId,
cartId: cartOneId,
paymentMethodId: 2,
orderDate: new Date("2024-01-15"),
status: "canceled",
shippingProcess : "The Order is canceled",
expectedDeliveryDate: new Date("2024-08-05"),
createdAt: new Date(),
updatedAt: new Date()
},
{
id: orderFourId,
products:JSON.stringify( [
{
productId:productOneId,
status:"pending"
},
{
productId:productTwoId,
status:"pending"
}
]),
shopId: shopTwoId,
cartId: cartOneId,
paymentMethodId: 2,
orderDate: new Date("2024-05-15"),
status: "canceled",
shippingProcess : "The Order is canceled",
expectedDeliveryDate: new Date("2024-08-05"),
createdAt: new Date(),
updatedAt: new Date()
},
{
id: orderFiveId,
products:JSON.stringify( [
{
productId:productOneId,
status:"pending"
},
{
productId:productTwoId,
status:"pending"
}
]),
shopId: shopTwoId,
cartId: cartOneId,
paymentMethodId: 2,
orderDate: new Date("2024-02-15"),
status: "completed",
shippingProcess : "your order have reached Kigali in 30 minutes it will be reached to you",
expectedDeliveryDate: new Date("2024-08-05"),
createdAt: new Date(),
updatedAt: new Date()
},
{
id: orderSixId,
products:JSON.stringify( [
{
productId:productOneId,
status:"pending"
},
{
productId:productTwoId,
status:"pending"
}
]),
shopId: shopTwoId,
cartId: cartOneId,
paymentMethodId: 2,
orderDate: new Date("2024-07-15"),
status: "completed",
shippingProcess : "your order have reached Kigali in 30 minutes it will be reached to you",
expectedDeliveryDate: new Date("2024-08-05"),
createdAt: new Date(),
updatedAt: new Date()
},
{
id: orderSevenId,
products:JSON.stringify( [
{
productId:productOneId,
status:"pending"
},
{
productId:productTwoId,
status:"pending"
}
]),
shopId: shopTwoId,
cartId: cartOneId,
paymentMethodId: 2,
orderDate: new Date("2024-07-15"),
status: "canceled",
shippingProcess : "The Order is canceled",
expectedDeliveryDate: new Date("2024-08-05"),
createdAt: new Date(),
updatedAt: new Date()
},
{
id: orderEightId,
products:JSON.stringify( [
{
productId:productOneId,
status:"pending"
},
{
productId:productTwoId,
status:"pending"
}
]),
shopId: shopTwoId,
cartId: cartOneId,
paymentMethodId: 2,
orderDate: new Date("2024-07-15"),
status: "canceled",
shippingProcess : "The Order is canceled",
expectedDeliveryDate: new Date("2024-08-05"),
createdAt: new Date(),
updatedAt: new Date()
},
{
id: orderNineId,
products:JSON.stringify( [
{
productId:productOneId,
status:"pending"
},
{
productId:productTwoId,
status:"pending"
}
]),
shopId: shopTwoId,
cartId: cartOneId,
paymentMethodId: 2,
orderDate: new Date("2024-08-02"),
status: "canceled",
shippingProcess : "The Order is canceled",
expectedDeliveryDate: new Date("2024-08-05"),
createdAt: new Date(),
updatedAt: new Date()
},
], {});
},

Expand Down
143 changes: 142 additions & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ describe("checkPasswordExpiration middleware", () => {
expect(sendEmailStub).to.have.been.calledOnceWith(
"[email protected]",
"Password Expired - Reset Required",
`Your password has expired. Please reset your password using the following link: ${process.env.SERVER_URL_PRO}/api/auth/reset-password`
`Your password has expired. Please reset your password using the following link: ${process.env.SERVER_URL_PRO}/reset-password`
);
expect(res.status).to.have.been.calledWith(httpStatus.FORBIDDEN);
expect(res.json).to.have.been.calledWith({
Expand Down Expand Up @@ -394,3 +394,144 @@ describe("checkPasswordExpiration middleware", () => {
expect(next).to.not.have.been.called;
});
});



import { Request, Response } from 'express';



const paymentSuccess = (req: Request, res: Response) => {
try {
res.status(httpStatus.OK).json({ status: httpStatus.OK, message: "Payment successful!" });
} catch (error) {
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ status: httpStatus.INTERNAL_SERVER_ERROR, message: error.message });
}
};

const paymentCanceled = (req: Request, res: Response) => {
try {
res.status(httpStatus.OK).json({ status: httpStatus.OK, message: "Payment canceled" });
} catch (error) {
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ status: httpStatus.INTERNAL_SERVER_ERROR, message: error.message });
}
};

describe('Payment Controllers', () => {
let req: Partial<Request>;
let res: Partial<Response>;
let statusStub: sinon.SinonStub;
let jsonStub: sinon.SinonStub;

beforeEach(() => {
req = {};
res = {
status: sinon.stub().returnsThis(),
json: sinon.stub(),
};
statusStub = res.status as sinon.SinonStub;
jsonStub = res.json as sinon.SinonStub;
});

afterEach(() => {
sinon.restore();
});

describe('paymentSuccess', () => {
it('should return 200 with success message', () => {
paymentSuccess(req as Request, res as Response);

expect(statusStub.calledOnceWith(httpStatus.OK)).to.be.true;
expect(jsonStub.calledOnceWith({ status: httpStatus.OK, message: "Payment successful!" })).to.be.true;
});
});

describe('paymentCanceled', () => {
it('should return 200 with canceled message', () => {
paymentCanceled(req as Request, res as Response);

expect(statusStub.calledOnceWith(httpStatus.OK)).to.be.true;
expect(jsonStub.calledOnceWith({ status: httpStatus.OK, message: "Payment canceled" })).to.be.true;
});
});
});










const userRepositories = {
findAddressByUserId: sinon.stub(),
addUserAddress: sinon.stub(),
updateUserAddress: sinon.stub(),
};

const changeUserAddress = async (req: any, res: Response) => {
try {
const isAddressFound = await userRepositories.findAddressByUserId(req.user.id);
let createdAddress;
if (!isAddressFound) {
createdAddress = await userRepositories.addUserAddress({ ...req.body, userId: req.user.id });
} else {
createdAddress = await userRepositories.updateUserAddress(req.body, req.user.id);
}
return res
.status(httpStatus.OK)
.json({
status: httpStatus.OK,
message: `${isAddressFound ? "Address updated successfully" : "Address added successfully"}`,
data: { address: createdAddress },
});
} catch (error) {
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
status: httpStatus.INTERNAL_SERVER_ERROR,
message: error.message,
});
}
};

describe('changeUserAddress', () => {
let req: Partial<Request>;
let res: Partial<Response>;
let statusStub: sinon.SinonStub;
let jsonStub: sinon.SinonStub;

beforeEach(() => {
req = {
user: { id: 'user123' },
body: { street: '123 Main St', city: 'Sample City' },
};
res = {
status: sinon.stub().returnsThis(),
json: sinon.stub(),
};
statusStub = res.status as sinon.SinonStub;
jsonStub = res.json as sinon.SinonStub;
});

afterEach(() => {
sinon.restore();
});

it('should add a new address if not found', async () => {
userRepositories.findAddressByUserId.resolves(null);
userRepositories.addUserAddress.resolves({ id: 'address123', ...req.body });

await changeUserAddress(req as Request, res as Response);

expect(userRepositories.findAddressByUserId.calledOnceWith(req.user.id)).to.be.true;
expect(userRepositories.addUserAddress.calledOnceWith({ ...req.body, userId: req.user.id })).to.be.true;
expect(userRepositories.updateUserAddress.notCalled).to.be.true;
expect(statusStub.calledOnceWith(httpStatus.OK)).to.be.true;
expect(jsonStub.calledOnceWith({
status: httpStatus.OK,
message: 'Address added successfully',
data: { address: { id: 'address123', ...req.body } },
})).to.be.true;
});
});
2 changes: 1 addition & 1 deletion src/middlewares/passwordExpiryCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface ExtendedRequest extends Request {
}

const PASSWORD_EXPIRATION_MINUTES = Number(process.env.PASSWORD_EXPIRATION_MINUTES) || 90;
const PASSWORD_RESET_URL = `${process.env.SERVER_URL_PRO}/api/auth/reset-password`;
const PASSWORD_RESET_URL = `${process.env.SERVER_URL_PRO}/reset-password`;

const addMinutes = (date: Date, minutes: number): Date => {
const result = new Date(date);
Expand Down
Loading

0 comments on commit b5d7201

Please sign in to comment.