services
parent
da0814bcf5
commit
24202861e5
|
@ -160,7 +160,7 @@ export async function UpdateStoreService(req: Request, res: Response) {
|
|||
|
||||
export async function CreateStoreServiceActivity(req: Request, res: Response) {
|
||||
try {
|
||||
const { serviceId, name, description, price, duration } = req.body;
|
||||
const { serviceId, name, description, price, duration, userIds } = req.body;
|
||||
|
||||
if (
|
||||
!serviceId ||
|
||||
|
@ -217,6 +217,16 @@ export async function CreateStoreServiceActivity(req: Request, res: Response) {
|
|||
duration: duration,
|
||||
});
|
||||
|
||||
if (userIds) {
|
||||
// add users to the activity
|
||||
userIds.forEach(async (userId: string) => {
|
||||
await StoreServiceActivityUsers.create({
|
||||
activity_id: activity.activity_id,
|
||||
user_id: userId,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).send({ activity });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
@ -237,19 +247,15 @@ export async function GetStoreServiceActivities(req: Request, res: Response) {
|
|||
service_id: serviceId,
|
||||
},
|
||||
attributes: ["activity_id", "name", "description", "price", "duration"],
|
||||
include: [
|
||||
{
|
||||
model: StoreServiceActivityUsers,
|
||||
attributes: ["user_id"],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
/*
|
||||
activities.forEach(async (activity) => {
|
||||
const assignedUsers = await StoreServiceActivityUsers.findAll({
|
||||
where: {
|
||||
activity_id: activity.activity_id,
|
||||
},
|
||||
attributes: ["user_id"],
|
||||
});
|
||||
}); */
|
||||
|
||||
res.status(200).send({ activities });
|
||||
res.status(200).send({ activities: activities });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).send({ err: "invalid request" });
|
||||
|
@ -258,20 +264,60 @@ export async function GetStoreServiceActivities(req: Request, res: Response) {
|
|||
|
||||
export async function UpdateStoreServiceActivity(req: Request, res: Response) {
|
||||
try {
|
||||
const { activityId, name, description, price, duration } = req.body;
|
||||
const { activityId, name, description, price, duration, userIds } =
|
||||
req.body;
|
||||
|
||||
if (
|
||||
!activityId ||
|
||||
!name ||
|
||||
!isStoreServiceActivityNameValid(name) ||
|
||||
!isStoreServiceActivityDescriptionValid(description) ||
|
||||
!isStoreServiceActivityPriceValid(price) ||
|
||||
!isStoreServiceActivityDurationValid(duration)
|
||||
) {
|
||||
if (!activityId) {
|
||||
logger.debug("Invalid request");
|
||||
return res.status(400).send({ err: "invalid request" });
|
||||
}
|
||||
|
||||
let update = {};
|
||||
|
||||
if (name) {
|
||||
if (!isStoreServiceActivityNameValid(name)) {
|
||||
return res.status(400).send({ err: "invalid request" });
|
||||
}
|
||||
|
||||
update = {
|
||||
...update,
|
||||
name: name,
|
||||
};
|
||||
}
|
||||
|
||||
if (description) {
|
||||
if (!isStoreServiceActivityDescriptionValid(description)) {
|
||||
return res.status(400).send({ err: "invalid request" });
|
||||
}
|
||||
|
||||
update = {
|
||||
...update,
|
||||
description: description,
|
||||
};
|
||||
}
|
||||
|
||||
if (price) {
|
||||
if (!isStoreServiceActivityPriceValid(price)) {
|
||||
return res.status(400).send({ err: "invalid request" });
|
||||
}
|
||||
|
||||
update = {
|
||||
...update,
|
||||
price: price,
|
||||
};
|
||||
}
|
||||
|
||||
if (duration) {
|
||||
if (!isStoreServiceActivityDurationValid(duration)) {
|
||||
return res.status(400).send({ err: "invalid request" });
|
||||
}
|
||||
|
||||
update = {
|
||||
...update,
|
||||
duration: duration,
|
||||
};
|
||||
}
|
||||
|
||||
// check if requester is the store owner
|
||||
|
||||
const userSession = await getUserSession(req);
|
||||
|
@ -316,19 +362,187 @@ export async function UpdateStoreServiceActivity(req: Request, res: Response) {
|
|||
|
||||
// update store service activity
|
||||
|
||||
await StoreServiceActivity.update(
|
||||
{
|
||||
name: name,
|
||||
description: description,
|
||||
price: price,
|
||||
duration: duration,
|
||||
},
|
||||
{
|
||||
if (Object.keys(update).length > 0) {
|
||||
await StoreServiceActivity.update(update, {
|
||||
where: {
|
||||
activity_id: activityId,
|
||||
},
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
if (userIds) {
|
||||
// remove all users from the activity
|
||||
await StoreServiceActivityUsers.destroy({
|
||||
where: {
|
||||
activity_id: activityId,
|
||||
},
|
||||
});
|
||||
|
||||
// add users to the activity
|
||||
userIds.forEach(async (userId: string) => {
|
||||
await StoreServiceActivityUsers.create({
|
||||
activity_id: activityId,
|
||||
user_id: userId,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).send({ msg: "success" });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).send({ err: "invalid request" });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DeleteStoreServiceActivity(req: Request, res: Response) {
|
||||
try {
|
||||
const { activityId } = req.params;
|
||||
|
||||
if (!activityId) {
|
||||
logger.debug("Invalid request");
|
||||
return res.status(400).send({ err: "invalid request" });
|
||||
}
|
||||
|
||||
// check if requester is the store owner
|
||||
|
||||
const userSession = await getUserSession(req);
|
||||
|
||||
if (!userSession) {
|
||||
return res.status(401).send({ err: "unauthorized" });
|
||||
}
|
||||
|
||||
const activity = await StoreServiceActivity.findOne({
|
||||
where: {
|
||||
activity_id: activityId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!activity) {
|
||||
return res.status(400).send({ err: "invalid request" });
|
||||
}
|
||||
|
||||
const service = await StoreService.findOne({
|
||||
where: {
|
||||
service_id: activity.service_id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!service) {
|
||||
return res.status(400).send({ err: "invalid request" });
|
||||
}
|
||||
|
||||
const store = await Store.findOne({
|
||||
where: {
|
||||
store_id: service.store_id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!store) {
|
||||
return res.status(400).send({ err: "invalid request" });
|
||||
}
|
||||
|
||||
if (store.owner_user_id !== userSession.user_id) {
|
||||
return res.status(401).send({ err: "unauthorized" });
|
||||
}
|
||||
|
||||
// delete store service activity
|
||||
|
||||
await StoreServiceActivity.destroy({
|
||||
where: {
|
||||
activity_id: activityId,
|
||||
},
|
||||
});
|
||||
|
||||
// delete all users from the activity
|
||||
|
||||
await StoreServiceActivityUsers.destroy({
|
||||
where: {
|
||||
activity_id: activityId,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).send({ msg: "success" });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).send({ err: "invalid request" });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DeleteStoreService(req: Request, res: Response) {
|
||||
try {
|
||||
const { serviceId } = req.params;
|
||||
|
||||
if (!serviceId) {
|
||||
logger.debug("Invalid request");
|
||||
return res.status(400).send({ err: "invalid request" });
|
||||
}
|
||||
|
||||
// check if requester is the store owner
|
||||
|
||||
const userSession = await getUserSession(req);
|
||||
|
||||
if (!userSession) {
|
||||
return res.status(401).send({ err: "unauthorized" });
|
||||
}
|
||||
|
||||
const service = await StoreService.findOne({
|
||||
where: {
|
||||
service_id: serviceId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!service) {
|
||||
return res.status(400).send({ err: "invalid request" });
|
||||
}
|
||||
|
||||
const store = await Store.findOne({
|
||||
where: {
|
||||
store_id: service.store_id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!store) {
|
||||
return res.status(400).send({ err: "invalid request" });
|
||||
}
|
||||
|
||||
if (store.owner_user_id !== userSession.user_id) {
|
||||
return res.status(401).send({ err: "unauthorized" });
|
||||
}
|
||||
|
||||
// get all store activities to delete the users from the activities
|
||||
|
||||
const activities = await StoreServiceActivity.findAll({
|
||||
where: {
|
||||
service_id: serviceId,
|
||||
},
|
||||
attributes: ["activity_id"],
|
||||
});
|
||||
|
||||
// delete all users from the activities
|
||||
|
||||
activities.forEach(async (activity) => {
|
||||
await StoreServiceActivityUsers.destroy({
|
||||
where: {
|
||||
activity_id: activity.activity_id,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
// delete all activities
|
||||
|
||||
await StoreServiceActivity.destroy({
|
||||
where: {
|
||||
service_id: serviceId,
|
||||
},
|
||||
});
|
||||
|
||||
// delete store service
|
||||
|
||||
await StoreService.destroy({
|
||||
where: {
|
||||
service_id: serviceId,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).send({ msg: "success" });
|
||||
} catch (error) {
|
||||
|
|
|
@ -133,7 +133,13 @@ export async function GetEmployees(req: Request, res: Response) {
|
|||
attributes: ["user_id", "username", "account_name"],
|
||||
});
|
||||
|
||||
res.status(200).send({ employees: employees });
|
||||
// filter out the requester from the employees
|
||||
|
||||
const filteredEmployees = employees.filter(
|
||||
(employee) => employee.user_id !== requesterSession.user_id
|
||||
);
|
||||
|
||||
res.status(200).send({ employees: filteredEmployees });
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
res.status(500).send({ err: "invalid request" });
|
||||
|
@ -225,17 +231,11 @@ export async function UpdateEmployee(req: Request, res: Response) {
|
|||
|
||||
// update user
|
||||
|
||||
await User.update(
|
||||
{
|
||||
username: username,
|
||||
account_name: accountName,
|
||||
await User.update(update, {
|
||||
where: {
|
||||
user_id: userId,
|
||||
},
|
||||
{
|
||||
where: {
|
||||
user_id: userId,
|
||||
},
|
||||
}
|
||||
)
|
||||
})
|
||||
.then(() => {
|
||||
res.status(200).send({ msg: "success" });
|
||||
})
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { DataTypes, Model } from "sequelize";
|
||||
import sequelize from "../database/database";
|
||||
import StoreServiceActivityUsers from "./storeServiceActivityUsers";
|
||||
|
||||
interface StoreServiceActivityAttributes {
|
||||
activity_id: string;
|
||||
|
@ -57,4 +58,9 @@ StoreServiceActivity.init(
|
|||
}
|
||||
);
|
||||
|
||||
// create associations
|
||||
StoreServiceActivity.hasMany(StoreServiceActivityUsers, {
|
||||
foreignKey: "activity_id",
|
||||
});
|
||||
|
||||
export default StoreServiceActivity;
|
||||
|
|
|
@ -15,5 +15,10 @@ router.post(
|
|||
"/activity/update",
|
||||
storeServicesController.UpdateStoreServiceActivity
|
||||
);
|
||||
router.delete(
|
||||
"/activity/:activityId",
|
||||
storeServicesController.DeleteStoreServiceActivity
|
||||
);
|
||||
router.delete("/:serviceId", storeServicesController.DeleteStoreService);
|
||||
|
||||
export default router;
|
||||
|
|
Loading…
Reference in New Issue