Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom fields #187

Draft
wants to merge 2 commits into
base: feature/vue3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions api/lib/controllers/custom-fields/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const CustomFieldsService = require('../../entities/custom-fields.service');

const {
schema,
includableFields,
} = require('../../entities/custom-fields.dto');

const { prepareStandardQueryParams } = require('../../services/std-query');

const standardQueryParams = prepareStandardQueryParams({
schema,
includableFields,
queryFields: ['id'],
});
exports.standardQueryParams = standardQueryParams;

exports.getMany = async (ctx) => {
const prismaQuery = standardQueryParams.getPrismaManyQuery(ctx);

const service = new CustomFieldsService();

ctx.type = 'json';
ctx.status = 200;
ctx.set('X-Total-Count', await service.count({ where: prismaQuery.where }));
ctx.body = await service.findMany(prismaQuery);
};

exports.getOne = async (ctx) => {
const prismaQuery = standardQueryParams.getPrismaOneQuery(ctx, { id: ctx.params.fieldId });

const service = new CustomFieldsService();
const customField = await service.findUnique(prismaQuery);

if (!customField) {
ctx.throw(404, ctx.$t('errors.customField.notFound'));
return;
}

ctx.type = 'json';
ctx.status = 200;
ctx.body = customField;
};

exports.upsertOne = async (ctx) => {
const { fieldId } = ctx.params;
const { body } = ctx.request;

const { upserted, existing } = await CustomFieldsService.$transaction(async (service) => ({
existing: await service.findUnique({
where: { id: fieldId },
}),
upserted: await service.upsert({
where: { id: fieldId },
create: { ...body, id: fieldId },
update: { ...body, id: fieldId },
}),
}));

ctx.status = existing ? 200 : 201;
ctx.body = upserted;
};

exports.deleteOne = async (ctx) => {
const { fieldId } = ctx.params;

const service = new CustomFieldsService();
await service.delete({ where: { id: fieldId } });

ctx.status = 204;
};
67 changes: 67 additions & 0 deletions api/lib/controllers/custom-fields/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const router = require('koa-joi-router')();
const { Joi } = require('koa-joi-router');
const {
requireJwt,
requireUser,
requireAdmin,
} = require('../../services/auth');

const {
standardQueryParams,
getMany,
getOne,
upsertOne,
deleteOne,
} = require('./actions');

const {
adminUpsertSchema,
} = require('../../entities/custom-fields.dto');

router.use(requireJwt, requireUser, requireAdmin);

router.get('/', {
method: 'GET',
path: '/',
handler: getMany,
validate: {
query: standardQueryParams.manyValidation,
},
});

router.route({
method: 'GET',
path: '/:fieldId',
handler: [
getOne,
],
validate: {
params: {
fieldId: Joi.string().trim().required(),
},
query: standardQueryParams.oneValidation,
},
});

router.route({
method: 'PUT',
path: '/:fieldId',
handler: upsertOne,
validate: {
type: 'json',
body: adminUpsertSchema,
},
});

router.route({
method: 'DELETE',
path: '/:fieldId',
handler: deleteOne,
validate: {
params: {
fieldId: Joi.string().trim().required(),
},
},
});

module.exports = router;
2 changes: 2 additions & 0 deletions api/lib/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const authorize = require('./auth');
const partners = require('./partners');
const metrics = require('./metrics');
const institutions = require('./institutions');
const customFields = require('./custom-fields');
const harvests = require('./harvests');
const harvestsSessions = require('./harvests-sessions');
const sushi = require('./sushi');
Expand Down Expand Up @@ -78,6 +79,7 @@ app.use(authorize.prefix('/profile').middleware());
app.use(logs.prefix('/logs').middleware());
app.use(files.prefix('/files').middleware());
app.use(institutions.prefix('/institutions').middleware());
app.use(customFields.prefix('/custom-fields').middleware());
app.use(harvests.prefix('/harvests').middleware());
app.use(harvestsSessions.prefix('/harvests-sessions').middleware());
app.use(sushi.prefix('/sushi').middleware());
Expand Down
Loading