Skip to content

Commit

Permalink
Merge pull request #377 from XLPeng57/#372-create-stakeholder-api-end…
Browse files Browse the repository at this point in the history
…point

feat: added /stakeholder/stakeholder_id endpoint
  • Loading branch information
dadiorchen authored Mar 23, 2024
2 parents 17d2ec1 + 9f2625b commit fa194cb
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import plantersRouter from './routers/plantersRouter';
import rawCapturesRouter from './routers/rawCapturesRouter';
import speciesRouter from './routers/speciesRouter';
import speciesRouterV2 from './routers/speciesRouterV2';
import stakeholderRouterV2 from './routers/stakeholderRouterV2';
import tokensRouter from './routers/tokensRouter';
import transactionsRouter from './routers/transactionsRouter';
import treesRouter from './routers/treesRouter';
Expand Down Expand Up @@ -82,6 +83,7 @@ app.use('/v2/trees', treesRouterV2);
app.use('/bounds', boundsRouter);
app.use('/gis', gisRouter);
app.use('/contract', contractsRouter);
app.use('/v2/stakeholder', stakeholderRouterV2);
// Global error handler
app.use(errorHandler);

Expand Down
26 changes: 26 additions & 0 deletions server/infra/database/StakeholderRepositoryV2.spec.ts
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',
});
});
});
24 changes: 24 additions & 0 deletions server/infra/database/StakeholderRepositoryV2.ts
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;
}
}
12 changes: 12 additions & 0 deletions server/interfaces/Stakeholder.ts
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;
}
7 changes: 7 additions & 0 deletions server/models/StakeholderV2.ts
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'),
};
22 changes: 22 additions & 0 deletions server/routers/stakeholderRouterV2.ts
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;

0 comments on commit fa194cb

Please sign in to comment.