149 lines
3.6 KiB
JavaScript
149 lines
3.6 KiB
JavaScript
import {
|
|
createAssignment,
|
|
updateAssignment,
|
|
getAssignmentsByQuery,
|
|
aggregateAssignment,
|
|
} from "../models/Assignment/operations.js";
|
|
import dayjs from "dayjs";
|
|
import { ObjectId } from "mongodb";
|
|
|
|
export const createAssignmentHandler = async (req, res) => {
|
|
try {
|
|
const { user } = res.locals;
|
|
const previousAssignment = await updateAssignment(req.body.venue, {
|
|
isActive: false,
|
|
});
|
|
const assignment = await createAssignment({
|
|
...req.body,
|
|
isActive: true,
|
|
createdOn: dayjs().toISOString(),
|
|
createdBy: user.id,
|
|
});
|
|
res.status(201).json({ result: { assignment, previousAssignment } });
|
|
} catch (error) {
|
|
res.status(500).json({ message: "Error creating assignment", error });
|
|
}
|
|
};
|
|
|
|
export const updateAssignmentHandler = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const assignment = await updateAssignment(id, req.body);
|
|
res.status(200).json({ result: assignment });
|
|
} catch (error) {
|
|
res.status(500).json({ message: "Error updating assignment", error });
|
|
}
|
|
};
|
|
|
|
export const getAssignmentsByUserHandler = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const pipeline = [
|
|
{
|
|
$match: {
|
|
user: ObjectId?.createFromHexString(id),
|
|
isActive: true,
|
|
},
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: "venues",
|
|
localField: "venue",
|
|
foreignField: "_id",
|
|
as: "venue",
|
|
},
|
|
},
|
|
{
|
|
$unwind: {
|
|
path: "$venue",
|
|
},
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: "partners",
|
|
localField: "venue.entity",
|
|
foreignField: "_id",
|
|
as: "partner",
|
|
},
|
|
},
|
|
{
|
|
$unwind: {
|
|
path: "$partner",
|
|
},
|
|
},
|
|
];
|
|
const assignment = await aggregateAssignment(pipeline);
|
|
res.status(200).json({ result: assignment });
|
|
} catch (error) {
|
|
res.status(500).json({ message: "Error getting assignment", error });
|
|
}
|
|
};
|
|
|
|
export const deactivateAssignmentHandler = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const assignment = await updateAssignment(id, { isActive: false });
|
|
res.status(200).json({ result: assignment });
|
|
} catch (error) {
|
|
res.status(500).json({ message: "Error deactivating assignment", error });
|
|
}
|
|
};
|
|
|
|
export const getAssignmentByVenueHandler = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const assignment = await getAssignmentsByQuery({ venue: id });
|
|
res.status(200).json({ result: assignment });
|
|
} catch (error) {
|
|
res.status(500).json({ message: "Error getting assignment", error });
|
|
}
|
|
};
|
|
|
|
export const getAssignmentByIdHandler = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
|
|
const pipeline = [
|
|
{
|
|
$match: {
|
|
_id: ObjectId.createFromHexString(id),
|
|
},
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: "venues",
|
|
localField: "venue",
|
|
foreignField: "_id",
|
|
as: "venue",
|
|
},
|
|
},
|
|
{
|
|
$unwind: {
|
|
path: "$venue",
|
|
preserveNullAndEmptyArrays: true,
|
|
},
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: "partners",
|
|
localField: "venue.entity",
|
|
foreignField: "_id",
|
|
as: "partner",
|
|
},
|
|
},
|
|
{
|
|
$unwind: {
|
|
path: "$partner",
|
|
preserveNullAndEmptyArrays: true,
|
|
},
|
|
},
|
|
];
|
|
|
|
const assignment = await aggregateAssignment(pipeline)[0];
|
|
|
|
res.status(200).json({ result: assignment });
|
|
} catch (error) {
|
|
res.status(500).json({ message: "Error getting assignment", error });
|
|
}
|
|
};
|