This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
210 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters