generated from Greenstand/treetracker-microservice-template
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…point feat: added /stakeholder/stakeholder_id endpoint
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,26 @@ | ||
import mockDb from 'mock-knex'; | ||
import Session from './Session'; | ||
import StakeholderRepositoryV2 from './StakeholderRepositoryV2'; | ||
|
||
describe('StakeholderRepositoryV2', () => { | ||
it('getById', async () => { | ||
const session = new Session(); | ||
mockDb.mock(session.getDB()); | ||
// eslint-disable-next-line | ||
var tracker = require('mock-knex').getTracker(); | ||
|
||
tracker.install(); | ||
tracker.on('query', (query) => { | ||
expect(query.sql).toBe( | ||
'select * from "stakeholder"."stakeholder" where "id" = $1 limit $2', | ||
); | ||
query.response([{ id: 'mock-uuid' }]); | ||
}); | ||
|
||
const repo = new StakeholderRepositoryV2(session); | ||
const result = await repo.getById('mock-uuid'); | ||
expect(result).toMatchObject({ | ||
id: 'mock-uuid', | ||
}); | ||
}); | ||
}); |
This file contains 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,24 @@ | ||
import Stakeholder from 'interfaces/Stakeholder'; | ||
import HttpError from 'utils/HttpError'; | ||
import BaseRepository from './BaseRepository'; | ||
import Session from './Session'; | ||
|
||
export default class StakeholderRepositoryV2 extends BaseRepository<Stakeholder> { | ||
constructor(session: Session) { | ||
super('stakeholder.stakeholder', session); | ||
} | ||
|
||
async getById(id: string | number) { | ||
const object = await this.session | ||
.getDB() | ||
.select() | ||
.from(this.tableName) | ||
.where('id', id) | ||
.first(); | ||
|
||
if (!object) { | ||
throw new HttpError(404, `Can not find ${this.tableName} by id:${id}!`); | ||
} | ||
return object; | ||
} | ||
} |
This file contains 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,12 @@ | ||
import DbModel from './DbModel'; | ||
|
||
export default interface Stakeholder extends DbModel { | ||
id: string; | ||
type: string; | ||
org_name: string; | ||
first_name: string; | ||
last_name: string; | ||
email: string; | ||
phone: string; | ||
website: string; | ||
} |
This file contains 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,7 @@ | ||
import Stakeholder from 'interfaces/Stakeholder'; | ||
import { delegateRepository } from '../infra/database/delegateRepository'; | ||
import StakeholderRepositoryV2 from '../infra/database/StakeholderRepositoryV2'; | ||
|
||
export default { | ||
getById: delegateRepository<StakeholderRepositoryV2, Stakeholder>('getById'), | ||
}; |
This file contains 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,22 @@ | ||
import express from 'express'; | ||
import Joi from 'joi'; | ||
import { handlerWrapper } from './utils'; | ||
import Session from '../infra/database/Session'; | ||
import StakeholderRepositoryV2 from '../infra/database/StakeholderRepositoryV2'; | ||
import StakeholderModel from '../models/StakeholderV2'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get( | ||
'/:id', | ||
handlerWrapper(async (req, res) => { | ||
Joi.assert(req.params.id, Joi.string().required()); | ||
const repo = new StakeholderRepositoryV2(new Session()); | ||
const exe = StakeholderModel.getById(repo); | ||
const result = await exe(req.params.id); | ||
res.send(result); | ||
res.end(); | ||
}), | ||
); | ||
|
||
export default router; |