Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Model {
  2.   constructor({ mongooseInstance }) {
  3.     if (!mongooseInstance)
  4.       throw new Error('Please provide a mongooseInstance to Model constructor')
  5.     this.mongooseInstance = mongooseInstance;
  6.  
  7.     this._addGenericFieldsToMongooseInstanceSchema()
  8.   }
  9.  
  10.   _addGenericFieldsToMongooseInstanceSchema() {
  11.     this.mongooseInstance.schema.add({
  12.       notes: {
  13.         type: [{
  14.           _user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
  15.           note: String,
  16.           created_at: {
  17.             type: Date,
  18.             default: Date.now,
  19.           },
  20.           updated_at: {
  21.             type: Date,
  22.             default: null,
  23.           },
  24.           updated_at: false,
  25.           bobdepannage: {
  26.             type: Boolean,
  27.             default: false
  28.           },
  29.           isPublic: {
  30.             type: Boolean,
  31.             default: true,
  32.           },
  33.           clientType: {
  34.             type: String,
  35.             default: 'client'
  36.           },
  37.         }],
  38.         default: []
  39.       },
  40.       created_at: {
  41.         type: Date,
  42.         label: 'Date de création',
  43.         isExportable: true,
  44.         default: Date.now,
  45.       },
  46.       updated_at: {
  47.         type: Date,
  48.         default: null,
  49.       },
  50.       deleted_at: {
  51.         type: Date,
  52.         default: null,
  53.         isExportable: true,
  54.         label: 'Date de supression',
  55.       },
  56.       deleted_by: {
  57.         type: mongoose.Schema.Types.ObjectId,
  58.         ref: "User",
  59.         default: null,
  60.         label: 'Supprimer par',
  61.         isExportable: true,
  62.         export: {
  63.           populate: {
  64.             path: 'delete_by',
  65.             select: 'firstName lastName',
  66.           },
  67.           value: ({ delete_by }) => delete_by
  68.             ? `${delete_by.firstName} ${delete_by.lastName}` : ''
  69.         }
  70.       },
  71.     })
  72.   }
  73.  
  74.   push({ _id = undefined, ...match }, path, data) {
  75.     return this.mongooseInstance
  76.     .findOneAndUpdate(
  77.       { _id, ...match },
  78.       { $push: { [path]: Array.isArray(data) ? { $in: data } : data }  },
  79.       opts
  80.     );
  81.   }
  82.  
  83.   pull({ _id = undefined, ...match }, path, data) {
  84.     return this.mongooseInstance
  85.     .findOneAndUpdate(
  86.       { _id, ...match },
  87.       { $pull: { [path]: Array.isArray(data) ? { $in: data } : data }  },
  88.       opts
  89.     );
  90.   }
  91.  
  92.   delete({ _id = undefined, ...match }, opts = { new: true }) {
  93.     return this.mongooseInstance
  94.       .findOneAndUpdate(
  95.         { _id, ...match },
  96.         { deleted_at: new Date(), deleted_by: user._id },
  97.         opts
  98.       );
  99.   }
  100.  
  101.   recycle({ _id = undefined, ...match }, opts = { new: true }) {
  102.     return this.mongooseInstance
  103.       .findOneAndUpdate(
  104.         { _id, ...match },
  105.         { deleted_at: null, deleted_by: null },
  106.         opts
  107.       );
  108.   }
  109.  
  110.   addNote({ _id = undefined, ...match }, { _user, note, isPublic, clientType }, opts = { new: true }) {
  111.     return this.mongooseInstance
  112.       .findOneAndUpdate(
  113.         { _id, ...match },
  114.         { $push: { notes: { _user, note, isPublic, clientType } } },
  115.         opts,
  116.       );
  117.   }
  118.  
  119.   updateNote({ _id = undefined, noteId, ...match }, note, opts = { new: true }) {
  120.     return this.mongooseInstance
  121.       .findOneAndUpdate(
  122.         { _id, 'notes._id': noteId, ...match },
  123.         { $set: { 'notes.$.note': note, updated_at: new Date() }},
  124.         opts,
  125.       );
  126.   }
  127.  
  128.   deleteNote({ _id = undefined, noteId, ...match }, opts = { new: true }) {
  129.     return this.mongooseInstance
  130.       .findOneAndUpdate(
  131.         { _id, ...match },
  132.         { $pull: { notes: { _id: noteId } }},
  133.         opts
  134.       );
  135.   }
  136. }
  137.  
  138. // models/tickets
  139.  
  140. export const Ticket = mongoose.model('Ticket', TicketSchema);
  141.  
  142. export default new Model({ mongooseInstance: Ticket })
  143.  
  144. // anywhere
  145.  
  146. import { Ticket } from 'models'
  147.  
  148. await Ticket.delete(_id, { new: false })
  149. const ticket = await Ticket.recycle(_id)
  150.  
  151. console.log(ticket.created_at)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement