553 lines
13 KiB
TypeScript
553 lines
13 KiB
TypeScript
import { Request, Response } from "express";
|
|
import StoreService from "../models/storeService";
|
|
import {
|
|
getUserSession,
|
|
newStoreServiceActivityId,
|
|
newStoreServiceId,
|
|
} from "../utils/utils";
|
|
import {
|
|
isStoreServiceActivityDescriptionValid,
|
|
isStoreServiceActivityDurationValid,
|
|
isStoreServiceActivityNameValid,
|
|
isStoreServiceActivityPriceValid,
|
|
isStoreServiceNameValid,
|
|
} from "../validator/validator";
|
|
import Store from "../models/store";
|
|
import logger from "../logger/logger";
|
|
import StoreServiceActivity from "../models/storeServiceActivity";
|
|
import StoreServiceActivityUsers from "../models/storeServiceActivityUsers";
|
|
import User from "../models/user";
|
|
|
|
export async function GetStoreServices(req: Request, res: Response) {
|
|
try {
|
|
const { storeId } = req.params;
|
|
|
|
if (!storeId) {
|
|
logger.debug("Invalid request");
|
|
return res.status(400).send({ err: "invalid request" });
|
|
}
|
|
|
|
const services = await StoreService.findAll({
|
|
where: {
|
|
store_id: storeId,
|
|
},
|
|
attributes: ["service_id", "name"],
|
|
});
|
|
|
|
// get all users assigned to the store
|
|
// this user list will be used in the ui to show if no users are selected for a service activity
|
|
|
|
const users = await User.findAll({
|
|
where: {
|
|
store_id: storeId,
|
|
},
|
|
attributes: ["user_id", "username"],
|
|
});
|
|
|
|
res.status(200).send({ services, users });
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(500).send({ err: "invalid request" });
|
|
}
|
|
}
|
|
|
|
export async function CreateStoreService(req: Request, res: Response) {
|
|
try {
|
|
const { storeId, name } = req.body;
|
|
|
|
if (!storeId || !name || !isStoreServiceNameValid(name)) {
|
|
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 store = await Store.findOne({
|
|
where: {
|
|
store_id: storeId,
|
|
},
|
|
});
|
|
|
|
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" });
|
|
}
|
|
|
|
// create store service
|
|
|
|
const service = await StoreService.create({
|
|
service_id: newStoreServiceId(),
|
|
store_id: storeId,
|
|
name: name,
|
|
});
|
|
|
|
res.status(200).send({ service });
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(500).send({ err: "invalid request" });
|
|
}
|
|
}
|
|
|
|
export async function UpdateStoreService(req: Request, res: Response) {
|
|
try {
|
|
const { serviceId, name } = req.body;
|
|
|
|
if (!serviceId || !name || !isStoreServiceNameValid(name)) {
|
|
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" });
|
|
}
|
|
|
|
// update store service
|
|
|
|
await StoreService.update(
|
|
{
|
|
name: name,
|
|
},
|
|
{
|
|
where: {
|
|
service_id: serviceId,
|
|
},
|
|
}
|
|
);
|
|
|
|
res.status(200).send({ msg: "success" });
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(500).send({ err: "invalid request" });
|
|
}
|
|
}
|
|
|
|
export async function CreateStoreServiceActivity(req: Request, res: Response) {
|
|
try {
|
|
const { serviceId, name, description, price, duration, userIds } = req.body;
|
|
|
|
if (
|
|
!serviceId ||
|
|
!name ||
|
|
!isStoreServiceActivityNameValid(name) ||
|
|
!isStoreServiceActivityDescriptionValid(description) ||
|
|
!isStoreServiceActivityPriceValid(price) ||
|
|
!isStoreServiceActivityDurationValid(duration)
|
|
) {
|
|
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" });
|
|
}
|
|
|
|
// create store service activity
|
|
|
|
const activity = await StoreServiceActivity.create({
|
|
activity_id: newStoreServiceActivityId(),
|
|
service_id: serviceId,
|
|
name: name,
|
|
description: description,
|
|
price: price,
|
|
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);
|
|
res.status(500).send({ err: "invalid request" });
|
|
}
|
|
}
|
|
|
|
export async function GetStoreServiceActivities(req: Request, res: Response) {
|
|
try {
|
|
const { storeId, serviceId } = req.params;
|
|
|
|
if (!storeId || !serviceId) {
|
|
return res.status(400).send({ err: "invalid request" });
|
|
}
|
|
|
|
const activities = await StoreServiceActivity.findAll({
|
|
where: {
|
|
service_id: serviceId,
|
|
},
|
|
attributes: ["activity_id", "name", "description", "price", "duration"],
|
|
include: [
|
|
{
|
|
model: StoreServiceActivityUsers,
|
|
attributes: ["user_id"],
|
|
},
|
|
],
|
|
});
|
|
|
|
res.status(200).send({ activities: activities });
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(500).send({ err: "invalid request" });
|
|
}
|
|
}
|
|
|
|
export async function UpdateStoreServiceActivity(req: Request, res: Response) {
|
|
try {
|
|
const { activityId, name, description, price, duration, userIds } =
|
|
req.body;
|
|
|
|
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);
|
|
|
|
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" });
|
|
}
|
|
|
|
// update store service activity
|
|
|
|
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) {
|
|
console.log(error);
|
|
res.status(500).send({ err: "invalid request" });
|
|
}
|
|
}
|