From 9f2625b8b499bed3bd07c098829febcaa8435192 Mon Sep 17 00:00:00 2001 From: xianglupeng Date: Sun, 10 Mar 2024 12:47:47 -0400 Subject: [PATCH] feat: added /stakeholder/stakeholder_id endpoint --- server/app.ts | 2 ++ .../database/StakeholderRepositoryV2.spec.ts | 26 +++++++++++++++++++ .../infra/database/StakeholderRepositoryV2.ts | 24 +++++++++++++++++ server/interfaces/Stakeholder.ts | 12 +++++++++ server/models/StakeholderV2.ts | 7 +++++ server/routers/stakeholderRouterV2.ts | 22 ++++++++++++++++ 6 files changed, 93 insertions(+) create mode 100644 server/infra/database/StakeholderRepositoryV2.spec.ts create mode 100644 server/infra/database/StakeholderRepositoryV2.ts create mode 100644 server/interfaces/Stakeholder.ts create mode 100644 server/models/StakeholderV2.ts create mode 100644 server/routers/stakeholderRouterV2.ts diff --git a/server/app.ts b/server/app.ts index b8ef64c..08663c7 100644 --- a/server/app.ts +++ b/server/app.ts @@ -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'; @@ -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); diff --git a/server/infra/database/StakeholderRepositoryV2.spec.ts b/server/infra/database/StakeholderRepositoryV2.spec.ts new file mode 100644 index 0000000..75faa29 --- /dev/null +++ b/server/infra/database/StakeholderRepositoryV2.spec.ts @@ -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', + }); + }); +}); diff --git a/server/infra/database/StakeholderRepositoryV2.ts b/server/infra/database/StakeholderRepositoryV2.ts new file mode 100644 index 0000000..e07d4ad --- /dev/null +++ b/server/infra/database/StakeholderRepositoryV2.ts @@ -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 { + 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; + } +} diff --git a/server/interfaces/Stakeholder.ts b/server/interfaces/Stakeholder.ts new file mode 100644 index 0000000..94742b4 --- /dev/null +++ b/server/interfaces/Stakeholder.ts @@ -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; +} diff --git a/server/models/StakeholderV2.ts b/server/models/StakeholderV2.ts new file mode 100644 index 0000000..7b6b787 --- /dev/null +++ b/server/models/StakeholderV2.ts @@ -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('getById'), +}; diff --git a/server/routers/stakeholderRouterV2.ts b/server/routers/stakeholderRouterV2.ts new file mode 100644 index 0000000..e4ee29a --- /dev/null +++ b/server/routers/stakeholderRouterV2.ts @@ -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;