Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Add methods to attachment services and cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
CeEv committed Mar 3, 2020
1 parent 98388cc commit cd52500
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/services/attachments/AttachmentModel.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const service = require('feathers-mongoose');
const { disallow } = require('feathers-hooks-common');

const { AttachmentModel } = require('./models/');
const { AttachmentModel } = require('./models');

const hooks = {};
hooks.before = {
Expand Down
71 changes: 46 additions & 25 deletions src/services/attachments/attachment.service.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/* eslint-disable class-methods-use-this */
const { Forbidden } = require('@feathersjs/errors');
const { Forbidden, NotFound } = require('@feathersjs/errors');

const logger = require('../../logger');
// const { validateSchema } = require('feathers-hooks-common');
// const Ajv = require('ajv');

// const { } = require('../../global/hooks');
const {
prepareParams,
permissions: permissionsHelper,
} = require('../../utils');
// const { } = require('./schemes');
// const { } = require('./hooks/');

const AttachmentServiceHooks = {
before: {
Expand All @@ -23,54 +21,77 @@ const AttachmentServiceHooks = {
// TODO: Schema
],
},
/* after: {
get: [
],
find: [
],
}, */
};

/**
* Attachments bind to the parents over target and targetModel.
* They can not get, or find over this external services.
*/
class AttachmentService {
constructor({ docs = {} } = {}) {
constructor({ docs = {}, baseService, typesToModelServices } = {}) {
if (!baseService || !typesToModelServices) {
logger.error('AttachmentService: missing params!', { baseService, typesToModelServices });
}
this.docs = docs;
this.baseService = baseService;
this.typesToModelServices = typesToModelServices;

this.err = Object.freeze({
noAccess: 'You have no access',
notFound: 'Element do not exist.',
});
}

setup(app) {
this.app = app;
}

populate(params) {
params.query.$populate = [
{ path: 'permissions.group', select: 'users' },
];
return params;
getTarget(id, params) {
return this.app.service(this.baseService)
.get(id, prepareParams(params, {
$select: ['target', 'targetModel'],
}))
.catch((err) => {
throw new NotFound(this.err.notFound, err);
});
}

async create(data, params) {
const internParams = this.populate(prepareParams(params));
const result = this.app.service(`models/${data.targetModel}`).get(data.target, internParams);
async hasPermission(target, targetModel, params) {
const modelServiceName = this.typesToModelServices[targetModel];
const result = await this.app.service(modelServiceName)
.get(target, prepareParams(params, {
$populate: [
{ path: 'permissions.group', select: 'users' },
],
}));

if (!result || !permissionsHelper.hasWrite(result.permissions, params.user)) {
throw new Forbidden(this.err.noAccess);
}
return data;
return result;
}

async create(data, params) {
await this.hasPermission(data.target, data.targetModel, params);
return this.app.service(this.baseService)
.create(data, prepareParams(params));
}

// target and targetModel is disallowed
async patch(id, data, params) {
// permissions
const { target, targetModel } = await this.getTarget(id, params);
await this.hasPermission(target, targetModel, params);
return this.app.service(this.baseService)
.patch(id, data, prepareParams(params));
}

async remove(id, params) {
// permissions
async remove(_id, params) {
const { target, targetModel } = await this.getTarget(_id, params);
await this.hasPermission(target, targetModel, params);
const deletedAt = new Date();
return this.app.service(this.baseService)
.patch(_id, { deletedAt }, prepareParams(params))
.then(() => ({ _id, deletedAt }));
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/services/attachments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ module.exports = (app) => {

const path = 'attachments';

app.use(path, new AttachmentService({}));
app.use(path, new AttachmentService({
baseService: '/models/AttachmentModel',
typesToModelServices: {
lesson: '/models/LessonModel',
section: '/models/SectionModel',
},
}));

const externerService = app.service(path);
externerService.hooks(AttachmentServiceHooks);
Expand Down
2 changes: 2 additions & 0 deletions src/services/sections/section.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ class SectionService {
// The query operation is also execute in mongoose after it is patched.
// But deletedAt exist as select and without mongoose.writeResult = true it return nothing.
const deletedAt = new Date();
// TODO: Maybe modifiedParamsToReturnPatchResponse and convertSuccessMongoPatchResponse can removed?
// -> Please show in attachment services deleted.
const patchParams = modifiedParamsToReturnPatchResponse(prepareParams(params));
const result = await service.patch(_id, { deletedAt }, patchParams)
.then((res) => convertSuccessMongoPatchResponse(res, { _id, deletedAt }, true));
Expand Down

0 comments on commit cd52500

Please sign in to comment.