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

Commit

Permalink
init attachment service
Browse files Browse the repository at this point in the history
  • Loading branch information
CeEv committed Mar 3, 2020
1 parent 512738f commit 98388cc
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 32 deletions.
41 changes: 41 additions & 0 deletions src/services/attachments/AttachmentModel.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const service = require('feathers-mongoose');
const { disallow } = require('feathers-hooks-common');

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

const hooks = {};
hooks.before = {
all: [
disallow('external'),
],
create: [
],
patch: [
],
remove: [
],
get: [
],
find: [
],
};

const AttachmentModelService = (app) => {
const option = {
Model: AttachmentModel,
lean: true,
paginate: {
default: 150,
max: 250,
},
whitelist: ['$elemMatch'],
};
app.use('models/AttachmentModel', service(option));
const modelService = app.service('models/AttachmentModel');
modelService.hooks(hooks);
};

module.exports = {
AttachmentModelService,
hooks,
};
81 changes: 81 additions & 0 deletions src/services/attachments/attachment.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-disable class-methods-use-this */
const { Forbidden } = require('@feathersjs/errors');
// 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: {
create: [
// TODO: Schema
],
patch: [
// TODO: Schema
],
remove: [
// 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 = {} } = {}) {
this.docs = docs;

this.err = Object.freeze({
noAccess: 'You have no access',
});
}

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

populate(params) {
params.query.$populate = [
{ path: 'permissions.group', select: 'users' },
];
return params;
}

async create(data, params) {
const internParams = this.populate(prepareParams(params));
const result = this.app.service(`models/${data.targetModel}`).get(data.target, internParams);
if (!result || !permissionsHelper.hasWrite(result.permissions, params.user)) {
throw new Forbidden(this.err.noAccess);
}
return data;
}

async patch(id, data, params) {
// permissions
}

async remove(id, params) {
// permissions
}
}


module.exports = {
AttachmentService,
AttachmentServiceHooks,
};
Empty file.
13 changes: 13 additions & 0 deletions src/services/attachments/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { AttachmentService, AttachmentServiceHooks } = require('./attachment.service');
const { AttachmentModelService } = require('./AttachmentModel.service');

module.exports = (app) => {
app.configure(AttachmentModelService);

const path = 'attachments';

app.use(path, new AttachmentService({}));

const externerService = app.service(path);
externerService.hooks(AttachmentServiceHooks);
};
41 changes: 41 additions & 0 deletions src/services/attachments/models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const mongoose = require('mongoose');

const { addTypeString } = require('../../../utils');

const { Schema } = mongoose;

const targetModels = ['lesson', 'section'];

const attachmentSchema = new Schema({
title: { type: String },
description: { type: String },
type: { type: String, required: true },
value: { type: Schema.Types.Mixed, default: null }, // Object, Number, String, Boolean
target: {
type: Schema.Types.ObjectId,
refPath: 'targetModel',
required: function requiredTarget() {
return !!this.targetModel;
},
},
targetModel: {
type: String,
enum: targetModels,
required: function requiredTargetModel() {
return !!this.target;
},
},
deletedAt: { type: Date, expires: (60 * 60 * 24 * 30) },
createdBy: { type: Schema.Types.ObjectId },
updatedBy: { type: Schema.Types.ObjectId },
}, {
timestamps: true,
});

attachmentSchema
.post('find', addTypeString('attachment'))
.post('findOne', addTypeString('attachment'));

module.exports = {
AttachmentModel: mongoose.model('attachment', attachmentSchema),
};
Empty file.
62 changes: 31 additions & 31 deletions src/services/groups/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
const { disallow } = require('feathers-hooks-common');

exports.before = {
all: [],
find: [],
get: [],
create: [],
update: [disallow()],
patch: [],
remove: [],
};

exports.after = {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: [],
};

exports.error = {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: [],
};
const { disallow } = require('feathers-hooks-common');

exports.before = {
all: [],
find: [],
get: [],
create: [],
update: [disallow()],
patch: [],
remove: [],
};

exports.after = {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: [],
};

exports.error = {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: [],
};
4 changes: 3 additions & 1 deletion src/services/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const lessons = require('./lessons');
const groups = require('./groups');
// const groups = require('./groups');
const syncGroups = require('./syncGroups');
const sections = require('./sections');
const viewports = require('./viewports');
const permissionsHelper = require('./permissionsHelper');
const attachments = require('./attachments');
const { systemInfo } = require('../logger');
const helpers = require('./helperServices');
// Events
Expand All @@ -16,6 +17,7 @@ module.exports = (app) => {
app.configure(sections);
app.configure(viewports);
app.configure(helpers);
app.configure(attachments);

app.configure(permissionsHelper.bind({
modelService: 'models/LessonModel',
Expand Down

0 comments on commit 98388cc

Please sign in to comment.