Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Model {
- constructor({ mongooseInstance }) {
- if (!mongooseInstance)
- throw new Error('Please provide a mongooseInstance to Model constructor')
- this.mongooseInstance = mongooseInstance;
- this._addGenericFieldsToMongooseInstanceSchema()
- }
- _addGenericFieldsToMongooseInstanceSchema() {
- this.mongooseInstance.schema.add({
- notes: {
- type: [{
- _user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
- note: String,
- created_at: {
- type: Date,
- default: Date.now,
- },
- updated_at: {
- type: Date,
- default: null,
- },
- updated_at: false,
- bobdepannage: {
- type: Boolean,
- default: false
- },
- isPublic: {
- type: Boolean,
- default: true,
- },
- clientType: {
- type: String,
- default: 'client'
- },
- }],
- default: []
- },
- created_at: {
- type: Date,
- label: 'Date de création',
- isExportable: true,
- default: Date.now,
- },
- updated_at: {
- type: Date,
- default: null,
- },
- deleted_at: {
- type: Date,
- default: null,
- isExportable: true,
- label: 'Date de supression',
- },
- deleted_by: {
- type: mongoose.Schema.Types.ObjectId,
- ref: "User",
- default: null,
- label: 'Supprimer par',
- isExportable: true,
- export: {
- populate: {
- path: 'delete_by',
- select: 'firstName lastName',
- },
- value: ({ delete_by }) => delete_by
- ? `${delete_by.firstName} ${delete_by.lastName}` : ''
- }
- },
- })
- }
- push({ _id = undefined, ...match }, path, data) {
- return this.mongooseInstance
- .findOneAndUpdate(
- { _id, ...match },
- { $push: { [path]: Array.isArray(data) ? { $in: data } : data } },
- opts
- );
- }
- pull({ _id = undefined, ...match }, path, data) {
- return this.mongooseInstance
- .findOneAndUpdate(
- { _id, ...match },
- { $pull: { [path]: Array.isArray(data) ? { $in: data } : data } },
- opts
- );
- }
- delete({ _id = undefined, ...match }, opts = { new: true }) {
- return this.mongooseInstance
- .findOneAndUpdate(
- { _id, ...match },
- { deleted_at: new Date(), deleted_by: user._id },
- opts
- );
- }
- recycle({ _id = undefined, ...match }, opts = { new: true }) {
- return this.mongooseInstance
- .findOneAndUpdate(
- { _id, ...match },
- { deleted_at: null, deleted_by: null },
- opts
- );
- }
- addNote({ _id = undefined, ...match }, { _user, note, isPublic, clientType }, opts = { new: true }) {
- return this.mongooseInstance
- .findOneAndUpdate(
- { _id, ...match },
- { $push: { notes: { _user, note, isPublic, clientType } } },
- opts,
- );
- }
- updateNote({ _id = undefined, noteId, ...match }, note, opts = { new: true }) {
- return this.mongooseInstance
- .findOneAndUpdate(
- { _id, 'notes._id': noteId, ...match },
- { $set: { 'notes.$.note': note, updated_at: new Date() }},
- opts,
- );
- }
- deleteNote({ _id = undefined, noteId, ...match }, opts = { new: true }) {
- return this.mongooseInstance
- .findOneAndUpdate(
- { _id, ...match },
- { $pull: { notes: { _id: noteId } }},
- opts
- );
- }
- }
- // models/tickets
- export const Ticket = mongoose.model('Ticket', TicketSchema);
- export default new Model({ mongooseInstance: Ticket })
- // anywhere
- import { Ticket } from 'models'
- await Ticket.delete(_id, { new: false })
- const ticket = await Ticket.recycle(_id)
- console.log(ticket.created_at)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement