Skip to content

Commit

Permalink
feat: app config
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed May 17, 2023
1 parent b766460 commit 5bd85e7
Show file tree
Hide file tree
Showing 14 changed files with 639 additions and 5 deletions.
4 changes: 2 additions & 2 deletions database/migrations/sqls/20230504224144-app-config-up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ CREATE TABLE app_config (
id uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
config_code text NOT NULL UNIQUE,
stakeholder_id uuid NOT NULL REFERENCES stakeholder(id),
capture_flow,
capture_setup_flow,
capture_flow jsonb,
capture_setup_flow jsonb,
active boolean DEFAULT true,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
Expand Down
Empty file.
158 changes: 158 additions & 0 deletions server/handlers/appConfigHandler/docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
const j2s = require('joi-to-swagger');
const {
appConfigPostSchema,
activateAppConfigSchema,
appConfigGetQuerySchema,
appInstallationsGetQuerySchema,
} = require('./schemas');

const { swagger: configPostSchema } = j2s(appConfigPostSchema);
const { swagger: configGetSchema } = j2s(appConfigGetQuerySchema);
const { swagger: installationGetSchema } = j2s(appInstallationsGetQuerySchema);
const { swagger: activateConfigSchema } = j2s(activateAppConfigSchema);

const appConfigResponse = {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/AppConfig',
},
},
},
};

const appConfigSwagger = {
'/app_config': {
get: {
tags: ['app_config'],
summary: 'get all app configs',
parameters: [
{
schema: {
...configGetSchema,
},
in: 'query',
name: 'query',
description: 'Allowed query parameters',
},
],
responses: {
200: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
appConfigs: {
type: 'array',
items: {
$ref: '#/components/schemas/AppConfig',
},
},
},
},
},
},
},
},
},
post: {
tags: ['app_config'],
summary: 'create a new app config',
requestBody: {
content: {
'application/json': {
schema: { ...configPostSchema },
},
},
},
responses: {
201: appConfigResponse,
},
},
},
'/app_installation': {
get: {
tags: ['app_config'],
summary: 'get all app installations',
parameters: [
{
schema: {
...installationGetSchema,
},
in: 'query',
name: 'query',
description: 'Allowed query parameters',
},
],
responses: {
200: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
appInstallations: {
type: 'array',
items: {
$ref: '#/components/schemas/AppInstallation',
},
},
},
},
},
},
},
},
},
},
'/activate_app_config': {
post: {
tags: ['app_config'],
summary: 'bind a wallet to an app_config',
requestBody: {
content: {
'application/json': {
schema: { ...activateConfigSchema },
},
},
},
responses: {
201: appConfigResponse,
},
},
},
};

const appConfigComponent = {
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
config_code: { type: 'string' },
stakeholder_id: { type: 'string', format: 'uuid' },
org_name: { type: 'string' },
capture_flow: { type: 'object' },
capture_setup_flow: { type: 'object' },
created_at: { type: 'string', format: 'date-time' },
updated_at: { type: 'string', format: 'date-time' },
app_installation_count: { type: 'number' },
},
};

const appInstallationComponent = {
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
wallet: { type: 'string' },
app_config_id: { type: 'string', format: 'uuid' },
config_code: { type: 'string' },
created_at: { type: 'string', format: 'date-time' },
latest_login_at: { type: 'string', format: 'date-time' },
},
};

module.exports = {
appConfigComponent,
appInstallationComponent,
appConfigSwagger,
};
98 changes: 98 additions & 0 deletions server/handlers/appConfigHandler/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const AppConfigService = require('../../services/AppConfigService');
const {
getFilterAndLimitOptions,
generatePrevAndNext,
} = require('../../utils/helper');
const {
appConfigPostSchema,
activateAppConfigSchema,
appConfigGetQuerySchema,
appInstallationsGetQuerySchema,
} = require('./schemas');

const appInstallationGet = async (req, res) => {
await appInstallationsGetQuerySchema.validateAsync(req.query, {
abortEarly: false,
});

const { filter, limitOptions } = getFilterAndLimitOptions(req.query);
const appConfigService = new AppConfigService();

const { appInstallations, totalCount } =
await appConfigService.getAppInstallations(filter, limitOptions);

const url = 'app_installation';

const links = generatePrevAndNext({
url,
count: totalCount,
limitOptions,
queryObject: { ...filter, ...limitOptions },
});

res.send({
appInstallations,
links,
totalCount,
query: { ...limitOptions, ...filter },
});
};

const appConfigGet = async (req, res) => {
await appConfigGetQuerySchema.validateAsync(req.query, {
abortEarly: false,
});

const { filter, limitOptions } = getFilterAndLimitOptions(req.query);
const appConfigService = new AppConfigService();

const { appConfigs, totalCount } = await appConfigService.getAppConfigs(
filter,
limitOptions,
);

const url = 'app_config';

const links = generatePrevAndNext({
url,
count: totalCount,
limitOptions,
queryObject: { ...filter, ...limitOptions },
});

res.send({
appConfigs,
links,
totalCount,
query: { ...limitOptions, ...filter },
});
};

const appConfigPost = async function (req, res) {
await appConfigPostSchema.validateAsync(req.body, {
abortEarly: false,
});

const appConfigService = new AppConfigService();
const result = await appConfigService.createAppConfig(req.body);

res.send(result);
};

const activateAppConfig = async function (req, res) {
await activateAppConfigSchema.validateAsync(req.body, {
abortEarly: false,
});

const appConfigService = new AppConfigService();
const result = await appConfigService.activateAppConfig(req.body);

res.send(result);
};

module.exports = {
appInstallationGet,
appConfigGet,
appConfigPost,
activateAppConfig,
};
35 changes: 35 additions & 0 deletions server/handlers/appConfigHandler/schemas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const Joi = require('joi');

const appConfigPostSchema = Joi.object({
stakeholder_id: Joi.string().uuid().required(),
config_code: Joi.string().alphanum().required(),
capture_flow: Joi.object(),
capture_setup_flow: Joi.object(),
});

const activateAppConfigSchema = Joi.object({
wallet: Joi.string().required(),
config_code: Joi.string().alphanum().required(),
});

const appConfigGetQuerySchema = Joi.object({
stakeholder_id: Joi.string().uuid(),
config_code: Joi.string().alphanum(),
limit: Joi.number().integer().min(1),
offset: Joi.number().integer().min(0),
});

const appInstallationsGetQuerySchema = Joi.object({
wallet: Joi.string(),
stakeholder_id: Joi.string().uuid(),
config_code: Joi.string().alphanum(),
limit: Joi.number().integer().min(1),
offset: Joi.number().integer().min(0),
});

module.exports = {
appConfigPostSchema,
activateAppConfigSchema,
appConfigGetQuerySchema,
appInstallationsGetQuerySchema,
};
2 changes: 1 addition & 1 deletion server/handlers/stakeholderHandler/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const stakeholderResponses = {
schema: {
type: 'object',
properties: {
tags: {
stakeholders: {
type: 'array',
items: {
$ref: '#/components/schemas/Stakeholder',
Expand Down
8 changes: 8 additions & 0 deletions server/handlers/swaggerDoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ const {
stakeholderSwagger,
stakeholderComponent,
} = require('./stakeholderHandler/docs');
const {
appConfigSwagger,
appConfigComponent,
appInstallationComponent,
} = require('./appConfigHandler/docs');

const { version } = require('../../package.json');

const paths = {
...stakeholderSwagger,
...appConfigSwagger,
};

const swaggerDefinition = {
Expand All @@ -18,7 +24,9 @@ const swaggerDefinition = {
paths,
components: {
schemas: {
AppConfig: { ...appConfigComponent },
Stakeholder: { ...stakeholderComponent },
AppInstallation: { ...appInstallationComponent },
},
},
};
Expand Down
Loading

0 comments on commit 5bd85e7

Please sign in to comment.