Skip to content

Commit

Permalink
Merge pull request #60 from SLIIT-Y3S2/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Sandaru-IT21001352 committed Oct 29, 2023
2 parents afe71b3 + 1072f1b commit abebd7a
Show file tree
Hide file tree
Showing 37 changed files with 1,423 additions and 173 deletions.
2 changes: 1 addition & 1 deletion backend/src/__tests__/delivery-for-site-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe("deliveries for site manager", () => {
});
});

// getting the deliveries for the site manger
// getting the deliveries for the site manager
describe("get deliveries for site manager route", () => {
describe("given the user is not logged in", () => {
it("should return a 403", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const userPayload = {
contactNumber: "0712345678",
};

describe("order", () => {
describe("order for company manager", () => {
beforeAll(async () => {
const mongoServer = await MongoMemoryServer.create();
await mongoose.connect(mongoServer.getUri());
Expand All @@ -53,77 +53,6 @@ describe("order", () => {
await mongoose.connection.close();
});

// for mobile application to display the list of suppliers
describe("get all suppliers", () => {
describe("given the user is not logged in", () => {
it("should return a 403", async () => {
const { statusCode } = await supertest(app).get("/api/suppliers");

expect(statusCode).toBe(403);
});
});
describe("given the user is logged in", () => {
it("should return a 200 and get all suppliers", async () => {
const jwt = signJwt(userPayload);

const { statusCode, body } = await supertest(app)
.get("/api/suppliers")
.set("Authorization", `Bearer ${jwt}`);

expect(statusCode).toBe(200);
expect(body).toEqual([]);
});
});
});

// for mobile application to display the list of items for a supplier
describe("get supplier item list", () => {
describe("given the user is not logged in", () => {
it("should return a 403", async () => {
const supplier = await createUser({
role: "supplier",
email: "supplier1@example.com",
password: "Password456!",
contactNumber: "0711345678",
name: "supplier1",
});

const { statusCode } = await supertest(app).get(
`/api/suppliers/${supplier._id}/items`
);

expect(statusCode).toBe(403);
});
});
describe("given the user is logged in", () => {
it("should return a 200 and get all supplier items", async () => {
const supplier = await createUser({
role: "supplier",
email: "supplier2@example.com",
password: "Password456!",
contactNumber: "0712345678",
name: "supplier2",
});

const item = await createItem({
name: "item2",
description: "item description",
price: 100,
supplier: supplier._id,
});
const jwt = signJwt(userPayload);

const { statusCode, body } = await supertest(app)
.get(`/api/suppliers/${supplier._id}/items`)
.set("Authorization", `Bearer ${jwt}`);

expect(statusCode).toBe(200);
expect(body[0].name).toEqual(item.name);
});
});
});

//for mobile application to create an order
describe("create order", () => {
describe("given the user is not logged in", () => {
it("should return a 403", async () => {
Expand Down
249 changes: 249 additions & 0 deletions backend/src/__tests__/order-for-site-manager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
import supertest from "supertest";
import { MongoMemoryServer } from "mongodb-memory-server";
import createServer from "../utils/server";
import mongoose from "mongoose";
import { signJwt } from "../utils/jwt.utils";
import { createUser } from "../service/user.service";
import { createItem } from "../service/item.service";
import { createSite } from "../service/site.service";

import { createOrder } from "../service/order.service";

// import { SiteInput } from "../models/site.model";
// import { createSite } from "../service/site.service";

const app = createServer();

const userId = new mongoose.Types.ObjectId().toString();

export const orderPayload = {
supplier: new mongoose.Types.ObjectId().toString(),
items: [
{
item: new mongoose.Types.ObjectId().toString(),
quantity: 2,
},
{
item: new mongoose.Types.ObjectId().toString(),
quantity: 5,
},
],
siteManager: userId,
site: new mongoose.Types.ObjectId().toString(),
comments: "comments",
dateToBeDelivered: new Date("2021-10-10 10:00:00").toString(),
};

export const userPayload = {
_id: userId,
email: "jane.doe@example.com",
name: "Jane Doe",
role: "siteManager",
contactNumber: "0712345678",
};

describe("order for site manager", () => {
beforeAll(async () => {
const mongoServer = await MongoMemoryServer.create();
await mongoose.connect(mongoServer.getUri());
});

afterAll(async () => {
await mongoose.disconnect();
await mongoose.connection.close();
});

// for mobile application to display the list of suppliers
describe("get all suppliers", () => {
describe("given the user is not logged in", () => {
it("should return a 403", async () => {
const { statusCode } = await supertest(app).get("/api/suppliers");

expect(statusCode).toBe(403);
});
});
describe("given the user is logged in", () => {
it("should return a 200 and get all suppliers", async () => {
const jwt = signJwt(userPayload);

const { statusCode, body } = await supertest(app)
.get("/api/suppliers")
.set("Authorization", `Bearer ${jwt}`);

expect(statusCode).toBe(200);
expect(body).toEqual([]);
});
});
});

// for mobile application to display the list of items for a supplier
describe("get supplier item list", () => {
describe("given the user is not logged in", () => {
it("should return a 403", async () => {
const supplier = await createUser({
role: "supplier",
email: "supplier1@example.com",
password: "Password456!",
contactNumber: "0711345678",
name: "supplier1",
});

const { statusCode } = await supertest(app).get(
`/api/suppliers/${supplier._id}/items`
);

expect(statusCode).toBe(403);
});
});
describe("given the user is logged in", () => {
it("should return a 200 and get all supplier items", async () => {
const supplier = await createUser({
role: "supplier",
email: "supplier2@example.com",
password: "Password456!",
contactNumber: "0712345678",
name: "supplier2",
});

const item = await createItem({
name: "item2",
description: "item description",
price: 100,
supplier: supplier._id,
});
const jwt = signJwt(userPayload);

const { statusCode, body } = await supertest(app)
.get(`/api/suppliers/${supplier._id}/items`)
.set("Authorization", `Bearer ${jwt}`);

expect(statusCode).toBe(200);
expect(body[0].name).toEqual(item.name);
});
});
});

//for mobile application to create an order
describe("create order", () => {
describe("given the user is not logged in", () => {
it("should return a 403", async () => {
const { statusCode } = await supertest(app).post("/api/orders");

expect(statusCode).toBe(403);
});
});
describe("given the user is logged in", () => {
it("should return a 200 and create order", async () => {
const jwt = signJwt(userPayload);

const supplier = await createUser({
role: "supplier",
email: "supplier3@example.com",
password: "Password456!",
contactNumber: "0712345678",
name: "supplier3",
});
const item1 = await createItem({
name: "item3",
description: "item description",
price: 100,
supplier: supplier._id,
});
const item2 = await createItem({
name: "item4",
description: "item description",
price: 100,
supplier: supplier._id,
});
const site = await createSite({
name: "site1",
address: "address",
contactNumber: "0712345678",
city: "city",
mapLocation: "mapLocation",
});
const { statusCode, body } = await supertest(app)
.post("/api/orders")

.send({
supplier: supplier._id,
items: [
{ item: item1._id, quantity: 2 },
{ item: item2._id, quantity: 5 },
],
siteManager: userId,
site: site._id,
comments: "comments",
dateToBeDelivered: new Date("2021-10-10 10:00:00") as Date,
})
.set("Authorization", `Bearer ${jwt}`);

expect(statusCode).toBe(201);

expect(body.status).toEqual("pending");
});
});
});

// view all their orders
describe("get all orders for site manager", () => {
describe("given the user is not logged in", () => {
it("should return a 403", async () => {
const { statusCode } = await supertest(app).get("/api/orders");

expect(statusCode).toBe(403);
});
});
describe("given the user is logged in", () => {
it("should return a 200 and get all orders", async () => {
const jwt = signJwt(userPayload);

const supplier = await createUser({
role: "supplier",
email: "supplier123@gmail.com",
contactNumber: "0712345678",
name: "supplier123",
password: "Password456!",
});
const item1 = await createItem({
name: "item123",
description: "item description",
price: 100,
supplier: supplier._id,
});
const item2 = await createItem({
name: "item456",
description: "item description",
price: 100,
supplier: supplier._id,
});
const site = await createSite({
name: "site123",
address: "address",
contactNumber: "0712345678",
city: "city",
mapLocation: "mapLocation",
});
await createOrder({
supplier: supplier._id,
items: [
{ item: item1._id, quantity: 2 },
{ item: item2._id, quantity: 5 },
],
siteManager: userId,
site: site._id,
comments: "comments",
dateToBeDelivered: new Date("2021-10-10 10:00:00").toDateString(),
});

const { statusCode, body } = await supertest(app)
.get("/api/orders/site-manager")
.set("Authorization", `Bearer ${jwt}`);

expect(statusCode).toBe(200);

expect(body).toHaveLength(2);
});
});
});
});
17 changes: 17 additions & 0 deletions backend/src/controller/order.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,20 @@ export async function declineOrderHandler(
return res.status(409).send(error.message);
}
}

// get all orders for a site manager
export async function getOrderListForSiteManagerHandler(
req: Request,
res: Response
) {
try {
const order = await getOrderList(
{ siteManager: res.locals.user._id },
true
);
return res.status(200).send(order);
} catch (error: any) {
logger.error(error);
return res.status(409).send(error.message);
}
}
Loading

0 comments on commit abebd7a

Please sign in to comment.