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

Commit

Permalink
Add schema, filter result.
Browse files Browse the repository at this point in the history
  • Loading branch information
Evers authored and Evers committed Mar 4, 2020
1 parent cd52500 commit 8a222f3
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/services/attachments/attachment.service.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { Forbidden, NotFound } = require('@feathersjs/errors');
const { validateSchema } = require('feathers-hooks-common');
const Ajv = require('ajv');

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

const {
prepareParams,
Expand All @@ -12,10 +13,10 @@ const {
const AttachmentServiceHooks = {
before: {
create: [
// TODO: Schema
validateSchema(createSchema, Ajv),
],
patch: [
// TODO: Schema
validateSchema(patchSchema, Ajv),
],
remove: [
// TODO: Schema
Expand Down Expand Up @@ -46,6 +47,12 @@ class AttachmentService {
this.app = app;
}

scopeParams(params) {
return prepareParams(params, {
$select: ['title', 'description', 'type', 'value'],
});
}

getTarget(id, params) {
return this.app.service(this.baseService)
.get(id, prepareParams(params, {
Expand Down Expand Up @@ -74,23 +81,25 @@ class AttachmentService {
async create(data, params) {
await this.hasPermission(data.target, data.targetModel, params);
return this.app.service(this.baseService)
.create(data, prepareParams(params));
.create(data, this.scopeParams(params));
}

// target and targetModel is disallowed
async patch(id, data, params) {
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));
.patch(id, data, this.scopeParams(params));
}

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))
.patch(_id, { deletedAt }, prepareParams(params, {
$select: '_id',
}))
.then(() => ({ _id, deletedAt }));
}
}
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/services/attachments/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { addTypeString } = require('../../../utils');

const { Schema } = mongoose;

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

const attachmentSchema = new Schema({
title: { type: String },
Expand Down
38 changes: 38 additions & 0 deletions src/services/attachments/schemas/attachment.create.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "attachmentCreate",
"required": [
"createdBy",
"type",
"target",
"targetModel"
],
"properties": {
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"type": {
"type": "string"
},
"value": {
"type": ["integer","string", "boolean", "object"]
},
"target": {
"type": "string",
"pattern": "[a-f0-9]{24}"
},
"targetModel": {
"type": "string",
"enum": ["lesson"]
},
"createdBy": {
"type": "string",
"pattern": "[a-f0-9]{24}"
}
},
"additionalProperties": false
}
24 changes: 24 additions & 0 deletions src/services/attachments/schemas/attachment.patch.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "attachmentCreate",
"required": [
"updatedBy"
],
"properties": {
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"value": {
"type": ["integer","string", "boolean", "object"]
},
"updatedBy": {
"type": "string",
"pattern": "[a-f0-9]{24}"
}
},
"additionalProperties": false
}
2 changes: 2 additions & 0 deletions src/services/attachments/schemas/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.createSchema = require('./attachment.create.schema.json');
exports.patchSchema = require('./attachment.patch.schema.json');

0 comments on commit 8a222f3

Please sign in to comment.