Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Company model #351

Merged
merged 2 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions backend/src/controllers/company.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const getCompanyFromReqBody = (body: any) => {
time_updated: body.time_updated,
location: body.location,
description: body.description,
image: body.image,
persons: body.persons,
};

Expand Down Expand Up @@ -58,19 +59,18 @@ export const createCompany = async (

const createdCompany = await companyService.createCompany(req.body);
logger.info(`${createdCompany._id}`);

createdCompany.persons.forEach((personInCompany) => {
if (!user.persons.includes(personInCompany)) {
companyService.deleteCompany(`${createdCompany._id}`);
res.status(httpStatus.FORBIDDEN).send('Person does not exist for this User').end();
}
});
if (createdCompany.persons) {
createdCompany.persons.forEach((personInCompany) => {
if (!user.persons.includes(personInCompany)) {
companyService.deleteCompany(`${createdCompany._id}`);
res.status(httpStatus.FORBIDDEN).send('Person does not exist for this User').end();
}
});
}

const updateUser = await userService.addCompanyToUser(user.auth_id, createdCompany._id);
const updatePerson = await personService
.addCompanyToPersons(createdCompany.persons, createdCompany._id);

if (!updateUser || !updatePerson) {
if (!updateUser) {
companyService.deleteCompany(`${createdCompany._id}`);
res.status(httpStatus.CONFLICT).send('Failed to add company').end();
}
Expand Down Expand Up @@ -108,6 +108,12 @@ export const updateCompany = async (
companyIdToUpdate,
newCompanyData,
);
// updating persons list
if (updatedCompany && updatedCompany.persons) {
const updatePerson = await personService
.addCompanyToPersons(updatedCompany.persons, updatedCompany._id);
}

return res
.sendStatus(
updatedCompany ? httpStatus.NO_CONTENT : httpStatus.NOT_FOUND,
Expand Down
8 changes: 8 additions & 0 deletions backend/src/controllers/person.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export const createPerson: POST = async (
if (!createdPerson) {
res.status(httpStatus.BAD_REQUEST).end();
} else {
await Promise.all(createdPerson.companies.map(async (companyId: any) => {
const company = await companyService.getCompany(companyId);
// add the person id to all its companies
if (company) {
company.persons.push(createdPerson._id);
}
}));

// Add a reference to the created person to the user
user = await userService.addPersonId(user.auth_id, createdPerson._id);
// If user doesn't contain reference to new person
Expand Down
4 changes: 3 additions & 1 deletion backend/src/models/company.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface CompanyModel {
description: string,
date_founded: Date,
time_updated: Date,
image: Buffer
persons: mongoose.Types.ObjectId[],
}

Expand All @@ -15,7 +16,8 @@ const schema = new Schema<CompanyModel>({
description: { type: String, required: false },
date_founded: { type: Date, required: false },
time_updated: { type: Date, default: new Date(Date.now()), required: true },
persons: { type: [mongoose.Types.ObjectId], required: true }
image: { type: Buffer, required: false },
persons: { type: [mongoose.Types.ObjectId], required: false },
});

export default model<CompanyModel>('Company', schema);
130 changes: 101 additions & 29 deletions backend/src/routes/__test__/company.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const companyData: CompanyModel = {
description: "Important stuff",
date_founded: new Date('2000-01-20'),
time_updated: new Date(Date.now()),
image: null as any,
persons: [] as any,
}

Expand All @@ -96,6 +97,7 @@ const company1Data: CompanyModel = {
description: "The Organisation sees everything",
date_founded: new Date('1990-01-20'),
time_updated: new Date(Date.now()),
image: Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]),
persons: [] as any,
}

Expand All @@ -105,6 +107,7 @@ const company1Data: CompanyModel = {
description: "Mystery",
date_founded: null as any,
time_updated: new Date(Date.now()),
image: Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]),
persons: [] as any,
}

Expand All @@ -113,6 +116,7 @@ const company1Data: CompanyModel = {
description: "Big Brother is watching you",
date_founded: new Date('1984-01-01'),
time_updated: new Date(Date.now()),
image: Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]),
persons: [] as any,
}

Expand All @@ -121,6 +125,7 @@ const company1Data: CompanyModel = {
location: "Mars",
date_founded: new Date('1980-10-20'),
time_updated: new Date(Date.now()),
image: Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]),
persons: [] as any,
}

Expand All @@ -130,25 +135,27 @@ const company1Data: CompanyModel = {
description: "Unnamed company",
date_founded: new Date('1030-8-20'),
time_updated: new Date(Date.now()),
image: Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]),
persons: [] as any,
}

const company6Data: CompanyModel = {
const company6Data = {
name: "Random Company",
location: "To be confirmed",
description: "Not much is known yet",
date_founded: new Date('2022-02-03'),
time_updated: new Date(Date.now()),
persons: null as any,
persons: [] as any,
}

const company7Data: CompanyModel = {
const company7Data = {
name: "Seven Seven Seven",
location: "Sevens",
description: "Seven",
date_founded: new Date('777-07-07'),
time_updated: new Date(Date.now()),
persons: [] as any,
image: Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]),
persons: null as any,
}

describe('POST /companies', () => {
Expand Down Expand Up @@ -248,7 +255,7 @@ describe('POST /companies', () => {
company4Data.persons = [];
})

it('Successful company creation return correct company data', async () => {
it('Successfully creating a company without an image field', async () => {
await supertest(app).post('/api/users')
.set('Accept', 'application/json')
.set('Authorization', token)
Expand All @@ -258,46 +265,51 @@ describe('POST /companies', () => {
.set('Accept', 'application/json')
.set('Authorization', token)
.send(person1Data)
.expect(httpStatus.CREATED);

company1Data.persons.push(person._id);
company6Data.persons.push(person._id);

const { body: storedcompany } = await supertest(app).post('/api/companies')
const { body: newcompany } = await supertest(app).post('/api/companies')
.set('Accept', 'application/json')
.set('Authorization', token)
.send(company1Data);
.send(company6Data)
.expect(httpStatus.CREATED);

expect(storedcompany.name).toEqual(company1Data.name);
expect(storedcompany.description).toEqual(company1Data.description);
expect(new Date(storedcompany.date_founded)).toEqual(company1Data.date_founded);
expect(storedcompany.location).toEqual(company1Data.location);
company1Data.persons.map((person) => {
expect(storedcompany.persons).toContain(person);
})
expect(newcompany.image).toEqual(undefined);

company1Data.persons = [];
company6Data.persons = [];
})

it('Failed to create a company without a persons field', async () => {
it('Successfully creating a company with empty persons field', async () => {
await supertest(app).post('/api/users')
.set('Accept', 'application/json')
.set('Authorization', token)
.send(user1Data);

await supertest(app).post('/api/persons')
const { body: newcompany } = await supertest(app).post('/api/companies')
.set('Accept', 'application/json')
.set('Authorization', token)
.send(person1Data);
.send(company6Data)
.expect(httpStatus.CREATED);

await supertest(app).post('/api/companies')
expect(newcompany.persons).toEqual([])
})

it('Successfully creating a company with a null persons field', async () => {
await supertest(app).post('/api/users')
.set('Accept', 'application/json')
.set('Authorization', token)
.send(company6Data)
.expect(httpStatus.BAD_REQUEST);
.send(user1Data);

const { body: newcompany } = await supertest(app).post('/api/companies')
.set('Accept', 'application/json')
.set('Authorization', token)
.send(company7Data)
.expect(httpStatus.CREATED);

expect(newcompany.persons).toEqual(null)
})

it('Failed to create a company with empty persons field', async () => {
it('Successful company creation return correct company data', async () => {
await supertest(app).post('/api/users')
.set('Accept', 'application/json')
.set('Authorization', token)
Expand All @@ -307,12 +319,24 @@ describe('POST /companies', () => {
.set('Accept', 'application/json')
.set('Authorization', token)
.send(person1Data)
.expect(httpStatus.CREATED);

await supertest(app).post('/api/companies')
company1Data.persons.push(person._id);

const { body: storedcompany } = await supertest(app).post('/api/companies')
.set('Accept', 'application/json')
.set('Authorization', token)
.send(company7Data)
.expect(httpStatus.BAD_REQUEST);
.send(company1Data);

expect(storedcompany.name).toEqual(company1Data.name);
expect(storedcompany.description).toEqual(company1Data.description);
expect(new Date(storedcompany.date_founded)).toEqual(company1Data.date_founded);
expect(storedcompany.location).toEqual(company1Data.location);
company1Data.persons.map((person) => {
expect(storedcompany.persons).toContain(person);
})

company1Data.persons = [];
})

it('Failed to create an company without a name', async () => {
Expand All @@ -326,15 +350,15 @@ describe('POST /companies', () => {
.set('Authorization', token)
.send(person1Data);

company7Data.persons.push(person._id);
company5Data.persons.push(person._id);

await supertest(app).post('/api/companies')
.set('Authorization', token)
.set('Accept', 'application/json')
.send(company5Data)
.expect(httpStatus.BAD_REQUEST);

company7Data.persons = [];
company5Data.persons = [];
})

it('Failed to create without an auth token', async () => {
Expand Down Expand Up @@ -467,6 +491,54 @@ describe('PUT /companies/:id ', () => {
expect(updatedcompany._body.location).toEqual(company2Data.location)
});

it('Successfully add people to a company', async () => {
await supertest(app).post('/api/users')
.set('Accept', 'application/json')
.set('Authorization', token)
.send(user1Data);

const { body: newcompany } = await supertest(app).post('/api/companies')
.set('Accept', 'application/json')
.set('Authorization', token)
.send(company1Data)
.expect(httpStatus.CREATED);

// create people to add
const person = await supertest(app)
.post('/api/persons')
.set('Accept', 'application/json')
.set('Authorization', token)
.send(person1Data)

const secondPerson = await supertest(app)
.post('/api/persons')
.set('Accept', 'application/json')
.set('Authorization', token)
.send(person2Data)

company1Data.persons = [person._body._id, secondPerson._body._id];

await supertest(app)
.put(`/api/companies/${newcompany._id}`)
.set('Accept', 'application/json')
.set('Authorization', token)
.send(company1Data)
.expect(httpStatus.NO_CONTENT) // since it's no content, need to get this company to check updated fields

// retrieve the updated company and compare
const { body: updatedcompany } = await supertest(app)
.get(`/api/companies/${newcompany._id}`)
.set('Accept', 'application/json')
.set('Authorization', token)
.send(company1Data)
.expect(httpStatus.OK)

expect(updatedcompany.persons.length).toBe(2)
expect(updatedcompany.persons[0]._id).toEqual(company1Data.persons[0])
expect(updatedcompany.persons[1]._id).toEqual(company1Data.persons[1])
company1Data.persons = [];
});

it('Fails when called with invalid company object ID', async () => {
await createUserPersonCompany(token)
// update an company of id that does not exist
Expand Down
35 changes: 35 additions & 0 deletions backend/src/routes/__test__/person.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ const companyData: CompanyModel = {
description: "Important stuff",
date_founded: new Date('2000-01-20'),
time_updated: new Date(Date.now()),
image: null as any,
persons: [] as any,
}

Expand Down Expand Up @@ -256,6 +257,40 @@ describe('POST persons/', () => {
expect(user.persons).toEqual([createdPerson._id]);
});

it ('Can be created containing an id from an existing company', async () => {
// Create a new user
await supertest(app).post('/api/users')
.set('Accept', 'application/json')
.set('Authorization', token)
.send(user1Data);

const { body: company } = await supertest(app).post('/api/companies')
.set('Accept', 'application/json')
.set('Authorization', token)
.send(companyData)
.expect(httpStatus.CREATED);

person1Data.companies.push(company._id);

// Create a new person who is already employed to the company
const { body: createdPerson } = await supertest(app).post('/api/persons')
.set('Accept', 'application/json')
.send(person1Data)
.set("Authorization", token)
.expect(httpStatus.CREATED);

// Ensure user contains reference to the new person
const { body: user } = await supertest(app).get("/api/users")
.set("Accept", "application/json")
.set("Authorization", token)
.expect(httpStatus.OK);

expect(user.persons).toEqual([createdPerson._id]);
expect(createdPerson.companies[0]).toEqual(company._id);

person1Data.companies = [];
});

it ('Can be created if "time_updated" is not provided', async() => {
// Create a new user
await supertest(app).post('/api/users')
Expand Down
Loading