Created
March 21, 2023 12:14
-
-
Save Avi-E-Koenig/6a68212aa67492b7cf0a8dad64b1d72f to your computer and use it in GitHub Desktop.
some chatgpt fun
This file contains hidden or 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
const { errMessage } = require('../errController'); | |
const Event = require('./event.model'); | |
const Setting = require('./setting.model'); | |
const aggregateQueryOld = [ | |
{ | |
$match: { | |
$expr: { | |
$and: [ | |
{ $isArray: '$targetAudience' }, | |
{ $ne: ['$targetAudience', []] }, | |
{ $ne: [{ $ifNull: ['$targetAudience', null] }, null] }, | |
], | |
}, | |
}, | |
}, | |
{ | |
$lookup: { | |
from: Setting.collection.name, | |
let: { targetAudience: '$targetAudience' }, | |
pipeline: [ | |
{ | |
$unwind: '$settingData', | |
}, | |
{ | |
$match: { | |
$expr: { | |
$in: ['$settingData._id', '$$targetAudience'], | |
}, | |
}, | |
}, | |
], | |
as: 'targetAudience', | |
}, | |
}, | |
{ | |
$project: { | |
_id: 1, | |
eventName: 1, | |
summary: 1, | |
advertiser: 1, | |
date: 1, | |
beginningTime: 1, | |
finishTime: 1, | |
place: 1, | |
category: 1, | |
targetAudience: '$targetAudience.settingData', | |
registrationPageURL: 1, | |
cardImageURL: 1, | |
coverImageURL: 1, | |
type: 1, | |
}, | |
}, | |
]; | |
const aggregateQuery = [ | |
{ | |
$match: { | |
$expr: { | |
$and: [ | |
{ $isArray: '$targetAudience' }, | |
{ $ne: ['$targetAudience', []] }, | |
{ $ne: [{ $ifNull: ['$targetAudience', null] }, null] }, | |
{ $isArray: '$category' }, | |
{ $ne: ['$category', []] }, | |
{ $ne: [{ $ifNull: ['$category', null] }, null] }, | |
], | |
}, | |
}, | |
}, | |
{ | |
$lookup: { | |
from: Setting.collection.name, | |
let: { targetAudience: '$targetAudience' }, | |
pipeline: [ | |
{ | |
$unwind: '$settingData', | |
}, | |
{ | |
$match: { | |
$expr: { | |
$in: ['$settingData._id', '$$targetAudience'], | |
}, | |
}, | |
}, | |
], | |
as: 'targetAudience', | |
}, | |
}, | |
{ | |
$lookup: { | |
from: Setting.collection.name, | |
let: { category: '$category' }, | |
pipeline: [ | |
{ | |
$unwind: '$settingData', | |
}, | |
{ | |
$match: { | |
$expr: { | |
$in: ['$settingData._id', '$$category'], | |
}, | |
}, | |
}, | |
], | |
as: 'category', | |
}, | |
}, | |
{ | |
$project: { | |
_id: 1, | |
eventName: 1, | |
summary: 1, | |
advertiser: 1, | |
date: 1, | |
beginningTime: 1, | |
finishTime: 1, | |
place: 1, | |
category: '$category.settingData', | |
targetAudience: '$targetAudience.settingData', | |
registrationPageURL: 1, | |
cardImageURL: 1, | |
coverImageURL: 1, | |
type: 1, | |
}, | |
}, | |
]; | |
async function create(data) { | |
return await Event.create(data); | |
} | |
async function read(filter) { | |
// return await eventsData.find(filter); | |
return await Event | |
// .find(filter) | |
.aggregate(aggregateQuery); | |
} | |
async function readOne(filter) { | |
const res = await Event.findOne(filter).aggregate(aggregateQuery); | |
if (!res) throw errMessage.EVENT_NOT_FOUND; | |
return res; | |
} | |
async function update(id, newData) { | |
return await Event.updateOne({ _id: id }, newData); | |
} | |
async function updateAndReturn(id, newData) { | |
let data = await Event.findOneAndUpdate({ _id: id }, newData, { | |
new: true, | |
}); | |
if (!data) throw errMessage.EVENT_NOT_FOUND; | |
return data; | |
} | |
async function updateAndReturnByAnyFilter(filter, newData) { | |
let data = await Event.findOneAndUpdate(filter, newData, { new: true }); | |
if (!data) throw errMessage.EVENT_NOT_FOUND; | |
return data; | |
} | |
async function del(id) { | |
return await update(id, { status: 'deleted' }); | |
} | |
module.exports = { | |
create, | |
read, | |
readOne, | |
update, | |
updateAndReturn, | |
updateAndReturnByAnyFilter, | |
del, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment