Skip to content

Commit

Permalink
fix: org return links
Browse files Browse the repository at this point in the history
  • Loading branch information
dadiorchen committed Jan 17, 2022
1 parent 3a053b6 commit c12f754
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions __tests__/e2e/organizations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ describe("organizations", () => {
expect(response.status).toBe(200);
expect(response.body).toMatchObject({
id: 1,
links: {
featured_trees: expect.stringMatching(/trees/),
associated_planters: expect.stringMatching(/planters/),
species: expect.stringMatching(/species/),
}
});
})

Expand All @@ -17,6 +22,11 @@ describe("organizations", () => {
expect(response.body.organizations).toBeInstanceOf(Array);
expect(response.body.organizations[0]).toMatchObject({
id: expect.any(Number),
links: {
featured_trees: expect.stringMatching(/trees/),
associated_planters: expect.stringMatching(/planters/),
species: expect.stringMatching(/species/),
}
})
}, 1000 * 30)

Expand Down
10 changes: 10 additions & 0 deletions server/models/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ function getByFilter(organizationRepository: OrganizationRepository): (filter: a
};
}

function getOrganizationLinks(organization){
const links = {
featured_trees : `/trees?organization_id=${organization.id}&limit=20&offset=0`,
associated_planters: `/planters?organization_id=${organization.id}&limit=20&offset=0`,
species: `/species?organization_id=${organization.id}&limit=20&offset=0`,
}
return links;
}

export default {
getById: delegateRepository(OrganizationRepository, "getById"),
getByFilter,
getOrganizationLinks,
}
4 changes: 3 additions & 1 deletion server/routers/organizationsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ router.get('/:id', handlerWrapper(async (req, res, next) => {
const repo = new OrganizationRepository(new Session());
const exe = OrganizationModel.getById(repo);
const result = await exe(req.params.id);
result.links = OrganizationModel.getOrganizationLinks(result);
res.send(result);
res.end();
}));
Expand Down Expand Up @@ -42,7 +43,8 @@ router.get('/', handlerWrapper(async (req, res, next) => {
total: null,
offset,
limit,
organizations: result,
organizations: result.map(organization => ({...organization, links: OrganizationModel.getOrganizationLinks(organization)})),

});
res.end();
}));
Expand Down

0 comments on commit c12f754

Please sign in to comment.