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: Date,
- 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,
- },
- 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}` : ''
- }
- },
- })
- }
- delete(_id, opts = { new: true }) {
- return this.mongooseInstance.findOneAndUpdate({ _id }, { deleted_at: new Date(), deleted_by: user._id }, opts);
- }
- recycle(_id, opts = { new: true }) {
- return this.mongooseInstance.findOneAndUpdate({ _id }, { deleted_at: null, deleted_by: null }, 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