-
Notifications
You must be signed in to change notification settings - Fork 56
DB endpoints #300
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
Merged
Merged
DB endpoints #300
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import _ from 'lodash'; | ||
import { middleware as tcMiddleware } from 'tc-core-library-js'; | ||
import util from '../../util'; | ||
import models from '../../models'; | ||
|
||
const permissions = tcMiddleware.permissions; | ||
|
||
module.exports = [ | ||
permissions('project.view'), | ||
async (req, res, next) => { | ||
const projectId = _.parseInt(req.params.projectId); | ||
const phaseId = _.parseInt(req.params.phaseId); | ||
|
||
// check if the project and phase are exist | ||
try { | ||
const countProject = await models.Project.count({ where: { id: projectId } }); | ||
if (countProject === 0) { | ||
const apiErr = new Error(`active project not found for project id ${projectId}`); | ||
apiErr.status = 404; | ||
throw apiErr; | ||
} | ||
|
||
const countPhase = await models.ProjectPhase.count({ where: { id: phaseId } }); | ||
if (countPhase === 0) { | ||
const apiErr = new Error(`active project phase not found for id ${phaseId}`); | ||
apiErr.status = 404; | ||
throw apiErr; | ||
} | ||
} catch (err) { | ||
return next(err); | ||
} | ||
|
||
const parameters = { | ||
projectId, | ||
phaseId, | ||
}; | ||
|
||
try { | ||
const { rows, count } = await models.PhaseProduct.search(parameters, req.log); | ||
return res.json(util.wrapResponse(req.id, rows, count)); | ||
} catch (err) { | ||
return next(err); | ||
} | ||
}, | ||
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
import _ from 'lodash'; | ||
import request from 'supertest'; | ||
import chai from 'chai'; | ||
import server from '../../app'; | ||
import models from '../../models'; | ||
import testUtil from '../../tests/util'; | ||
|
||
const should = chai.should(); | ||
|
||
const body = { | ||
name: 'test phase product', | ||
type: 'product1', | ||
estimatedPrice: 20.0, | ||
actualPrice: 1.23456, | ||
details: { | ||
message: 'This can be any json', | ||
}, | ||
createdBy: 1, | ||
updatedBy: 1, | ||
}; | ||
|
||
describe('Phase Products', () => { | ||
let projectId; | ||
let phaseId; | ||
let project; | ||
const memberUser = { | ||
handle: testUtil.getDecodedToken(testUtil.jwts.member).handle, | ||
userId: testUtil.getDecodedToken(testUtil.jwts.member).userId, | ||
firstName: 'fname', | ||
lastName: 'lName', | ||
email: 'some@abc.com', | ||
}; | ||
const copilotUser = { | ||
handle: testUtil.getDecodedToken(testUtil.jwts.copilot).handle, | ||
userId: testUtil.getDecodedToken(testUtil.jwts.copilot).userId, | ||
firstName: 'fname', | ||
lastName: 'lName', | ||
email: 'some@abc.com', | ||
}; | ||
before(function beforeHook(done) { | ||
this.timeout(10000); | ||
// mocks | ||
testUtil.clearDb() | ||
.then(() => { | ||
models.Project.create({ | ||
type: 'generic', | ||
billingAccountId: 1, | ||
name: 'test1', | ||
description: 'test project1', | ||
status: 'draft', | ||
details: {}, | ||
createdBy: 1, | ||
updatedBy: 1, | ||
lastActivityAt: 1, | ||
lastActivityUserId: '1', | ||
}).then((p) => { | ||
projectId = p.id; | ||
project = p.toJSON(); | ||
// create members | ||
models.ProjectMember.bulkCreate([{ | ||
id: 1, | ||
userId: copilotUser.userId, | ||
projectId, | ||
role: 'copilot', | ||
isPrimary: false, | ||
createdBy: 1, | ||
updatedBy: 1, | ||
}, { | ||
id: 2, | ||
userId: memberUser.userId, | ||
projectId, | ||
role: 'customer', | ||
isPrimary: true, | ||
createdBy: 1, | ||
updatedBy: 1, | ||
}]).then(() => { | ||
models.ProjectPhase.create({ | ||
name: 'test project phase', | ||
status: 'active', | ||
startDate: '2018-05-15T00:00:00Z', | ||
endDate: '2018-05-15T12:00:00Z', | ||
budget: 20.0, | ||
progress: 1.23456, | ||
details: { | ||
message: 'This can be any json', | ||
}, | ||
createdBy: 1, | ||
updatedBy: 1, | ||
projectId, | ||
}).then((phase) => { | ||
phaseId = phase.id; | ||
_.assign(body, { phaseId, projectId }); | ||
project.lastActivityAt = 1; | ||
project.phases = [phase.toJSON()]; | ||
|
||
models.PhaseProduct.create(body).then((product) => { | ||
project.phases[0].products = [product.toJSON()]; | ||
project.lastActivityAt = 1; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
after((done) => { | ||
testUtil.clearDb(done); | ||
}); | ||
|
||
describe('GET /projects/{id}/phases/{phaseId}/products/db', () => { | ||
it('should return 403 when user have no permission (non team member)', (done) => { | ||
request(server) | ||
.get(`/v4/projects/${projectId}/phases/${phaseId}/products/db`) | ||
.set({ | ||
Authorization: `Bearer ${testUtil.jwts.member2}`, | ||
}) | ||
.send({ param: body }) | ||
.expect('Content-Type', /json/) | ||
.expect(403, done); | ||
}); | ||
|
||
it('should return 404 when no project with specific projectId', (done) => { | ||
request(server) | ||
.get(`/v4/projects/999/phases/${phaseId}/products/db`) | ||
.set({ | ||
Authorization: `Bearer ${testUtil.jwts.manager}`, | ||
}) | ||
.send({ param: body }) | ||
.expect('Content-Type', /json/) | ||
.expect(404, done); | ||
}); | ||
|
||
it('should return 404 when no phase with specific phaseId', (done) => { | ||
request(server) | ||
.get(`/v4/projects/${projectId}/phases/99999/products/db`) | ||
.set({ | ||
Authorization: `Bearer ${testUtil.jwts.manager}`, | ||
}) | ||
.send({ param: body }) | ||
.expect('Content-Type', /json/) | ||
.expect(404, done); | ||
}); | ||
|
||
it('should return 1 phase when user have project permission (customer)', (done) => { | ||
request(server) | ||
.get(`/v4/projects/${projectId}/phases/${phaseId}/products/db`) | ||
.set({ | ||
Authorization: `Bearer ${testUtil.jwts.member}`, | ||
}) | ||
.send({ param: body }) | ||
.expect('Content-Type', /json/) | ||
.expect(200) | ||
.end((err, res) => { | ||
if (err) { | ||
done(err); | ||
} else { | ||
const resJson = res.body.result.content; | ||
should.exist(resJson); | ||
resJson.should.have.lengthOf(1); | ||
done(); | ||
} | ||
}); | ||
}); | ||
|
||
it('should return 1 phase when user have project permission (copilot)', (done) => { | ||
request(server) | ||
.get(`/v4/projects/${projectId}/phases/${phaseId}/products/db`) | ||
.set({ | ||
Authorization: `Bearer ${testUtil.jwts.copilot}`, | ||
}) | ||
.send({ param: body }) | ||
.expect('Content-Type', /json/) | ||
.expect(200) | ||
.end((err, res) => { | ||
if (err) { | ||
done(err); | ||
} else { | ||
const resJson = res.body.result.content; | ||
should.exist(resJson); | ||
resJson.should.have.lengthOf(1); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.