Skip to content

Commit

Permalink
feat: add query param to only return essential columns
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Dec 23, 2021
1 parent 69b8dda commit a21be7d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
49 changes: 38 additions & 11 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ export const findAll = async (req, res) => {
} else if (dialect === 'mysql') {
res.json(await Project.findAllMySQLFts(search, orgUid));
}
} else {
return;
}

if (req.query.onlyEssentialColumns) {
const query = {
include: [
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
attributes: [
'orgUid',
'warehouseProjectId',
'currentRegistry',
'registryOfOrigin',
'projectLink',
'projectStatus',
'projectTag',
],
};

Expand All @@ -61,10 +66,23 @@ export const findAll = async (req, res) => {
};
}
res.json(await Project.findAll(query));
return;
}

const query = {
include: [
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
],
};

res.json(await Project.findAll(query));
};

export const findOne = (req, res) => {
export const findOne = async (req, res) => {
if (req.query.useMock) {
const record = ProjectMock.findOne(req.query.id);
if (record) {
Expand All @@ -76,9 +94,18 @@ export const findOne = (req, res) => {
return;
}

res.json({
message: 'Not Yet Implemented',
});
const query = {
where: { warehouseProjectId: res.query.warehouseProjectId },
include: [
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
],
};

res.json(await Project.findOne(query));
};

export const update = async (req, res) => {
Expand Down
34 changes: 30 additions & 4 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ export const findAll = async (req, res) => {
return;
}

if (req.query.onlyEssentialColumns) {
res.json(
await Unit.findAll({
attributes: [
'orgUid',
'unitLink',
'registry',
'unitType',
'unitCount',
'unitStatus',
'unitStatusDate',
],
}),
);
return;
}

res.json(
await Unit.findAll({
include: [
Expand All @@ -44,7 +61,7 @@ export const findAll = async (req, res) => {
);
};

export const findOne = (req, res) => {
export const findOne = async (req, res) => {
if (req.query.useMock) {
const record = UnitMock.findOne(req.query.id);
if (record) {
Expand All @@ -56,9 +73,18 @@ export const findOne = (req, res) => {
return;
}

res.json({
message: 'Not Yet Implemented',
});
res.json(
await Unit.findAll({
where: { id: req.query.id },
include: [
{
model: Qualification,
as: 'qualification',
},
Vintage,
],
}),
);
};

export const update = async (req, res) => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/v1/resources/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const validator = joiExpress.createValidator({});
const ProjectRouter = express.Router();

ProjectRouter.get('/', (req, res) => {
return req.query.id
return req.query.warehouseProjectId
? ProjectController.findOne(req, res)
: ProjectController.findAll(req, res);
});
Expand Down

0 comments on commit a21be7d

Please sign in to comment.