send request when creating or removing a employee

main
alex 2024-02-11 10:25:41 +01:00
parent 8c269fd193
commit 981cb9757d
5 changed files with 116 additions and 93 deletions

View File

@ -3,10 +3,12 @@ import logger from "../logger/logger";
import Store from "../models/store";
import { decodeBase64, getUserSession, matchPassword } from "../utils/utils";
import User from "../models/user";
import UserGoogleTokens from "../models/userGoogleTokens";
import axios from "axios";
import { isPasswordValid } from "../validator/validator";
import { requestTerminPlanerChangeFutureBookingDays } from "../utils/terminPlaner";
import {
isTerminPlanerGoogleCalendarConnected,
terminPlanerRequest,
terminPlanerRequestChangeFutureBookingDays,
} from "../utils/terminPlaner";
// this request is needed to get the store id after the user has connected his calendar to redirect him back to the calendar page
export async function GetStoreId(req: Request, res: Response) {
@ -56,22 +58,20 @@ export async function GetCalendarSettings(req: Request, res: Response) {
// check if the user has the google calendar connected
const userGoogleToken = await UserGoogleTokens.findOne({
where: {
user_id: userSession.user_id,
},
attributes: ["status"],
});
const googleCalendarConnected = await isTerminPlanerGoogleCalendarConnected(
userSession.user_id,
["status"]
);
if (!userGoogleToken) {
if (!googleCalendarConnected) {
return res.status(200).send({
status: CalendarStatus.NOT_CONNECTED,
});
}
if (userGoogleToken.status !== CalendarStatus.OK) {
if (googleCalendarConnected.status !== CalendarStatus.OK) {
return res.status(200).send({
status: userGoogleToken.status,
status: googleCalendarConnected.status,
});
}
@ -138,14 +138,14 @@ export async function GetCalendarSettings(req: Request, res: Response) {
if (store.owner_user_id !== userSession.user_id) {
// user is not store owner - means he is an employee
res.status(200).send({
status: userGoogleToken.status,
status: googleCalendarConnected.status,
userSettings: userSettings,
});
return;
}
// user is store owner
res.status(200).send({
status: userGoogleToken.status,
status: googleCalendarConnected.status,
storeSettings: {
calendar_max_future_booking_days:
store.calendar_max_future_booking_days,
@ -187,13 +187,11 @@ export async function UpdatePersonalCalendarSettings(
// check if the user has the google calendar connected
const userGoogleToken = await UserGoogleTokens.findOne({
where: {
user_id: userSession.user_id,
},
});
const googleCalendarConnected = await isTerminPlanerGoogleCalendarConnected(
userSession.user_id
);
if (!userGoogleToken) {
if (!googleCalendarConnected) {
return res.status(401).send({ err: "unauthorized" });
}
@ -241,13 +239,11 @@ export async function UpdateStoreCalendarSettings(req: Request, res: Response) {
// check if the user has the google calendar connected
const userGoogleToken = await UserGoogleTokens.findOne({
where: {
user_id: userSession.user_id,
},
});
const googleCalendarConnected = await isTerminPlanerGoogleCalendarConnected(
userSession.user_id
);
if (!userGoogleToken) {
if (!googleCalendarConnected) {
return res.status(401).send({ err: "unauthorized" });
}
@ -281,7 +277,7 @@ export async function UpdateStoreCalendarSettings(req: Request, res: Response) {
calendar_min_earliest_booking_time: calendarMinEarliestBookingTime,
};
requestTerminPlanerChangeFutureBookingDays(store.store_id);
terminPlanerRequestChangeFutureBookingDays(store.store_id);
}
if (Object.keys(update).length === 0) {
@ -322,13 +318,11 @@ export async function UnlinkGoogleCalendar(req: Request, res: Response) {
// check if the user has the google calendar connected
const userGoogleToken = await UserGoogleTokens.findOne({
where: {
user_id: userSession.user_id,
},
});
const googleCalendarConnected = await isTerminPlanerGoogleCalendarConnected(
userSession.user_id
);
if (!userGoogleToken) {
if (!googleCalendarConnected) {
return res.status(401).send({ err: "unauthorized" });
}
@ -366,17 +360,11 @@ export async function UnlinkGoogleCalendar(req: Request, res: Response) {
// request to termin planner to remove google account
axios
.post(`${process.env.TERMIN_PLANNER_URL}/removeGoogleAccount` as string, {
userId: userSession.user_id,
pass: process.env.TERMIN_PLANNER_AUTHORIZATION_PASSWORD as string,
})
.then(() => res.status(200).send({ msg: "success" }))
.catch((err) => {
logger.info("err %s", err);
await terminPlanerRequest("/api/v1/removeGoogleAccount", "POST", {
userId: userSession.user_id,
});
res.status(500).send({ err: "invalid request" });
});
res.status(200).send({ msg: "success" });
} catch (error) {
logger.error(error);
res.status(500).send({ err: "invalid request" });

View File

@ -20,7 +20,11 @@ import {
USER_ANALYTICS_ENABLED_DEFAULT,
} from "../utils/constants";
import Store from "../models/store";
import { requestTerminPlanerChangeFutureBookingDays } from "../utils/terminPlaner";
import {
isTerminPlanerGoogleCalendarConnected,
terminPlanerRequest,
terminPlanerRequestChangeFutureBookingDays,
} from "../utils/terminPlaner";
export async function AddEmployee(req: Request, res: Response) {
try {
@ -141,12 +145,18 @@ export async function AddEmployee(req: Request, res: Response) {
};
}
await User.create(newUser)
.then(() => res.status(200).send({ msg: "success" }))
.catch((err) => {
logger.error(err);
res.status(500).send({ err: "invalid request" });
});
const googleCalendarConnected = await isTerminPlanerGoogleCalendarConnected(
store.owner_user_id
);
// only request terminplaner if google calendar is connected
if (googleCalendarConnected) {
await terminPlanerRequest("/api/v1/addUser", "POST", { userId: userId });
}
await User.create(newUser);
res.status(200).send({ msg: "success" });
} catch (error) {
logger.error(error);
res.status(500).send({ err: "invalid request" });
@ -313,7 +323,7 @@ export async function UpdateEmployee(req: Request, res: Response) {
calendar_min_earliest_booking_time: calendarMinEarliestBookingTime,
};
requestTerminPlanerChangeFutureBookingDays(store.store_id);
terminPlanerRequestChangeFutureBookingDays(store.store_id);
}
if (Object.keys(update).length === 0) {
@ -383,19 +393,24 @@ export async function DeleteEmployee(req: Request, res: Response) {
return res.status(400).send({ err: "invalid request" });
}
User.destroy({
const googleCalendarConnected = await isTerminPlanerGoogleCalendarConnected(
store.owner_user_id
);
// only request terminplaner if google calendar is connected
if (googleCalendarConnected) {
await terminPlanerRequest("/api/v1/removeUser", "POST", {
userId: userId,
});
}
await User.destroy({
where: {
user_id: userId,
},
})
.then(() => {
// delete user
res.status(200).send({ msg: "success" });
})
.catch((err) => {
logger.error(err);
res.status(500).send({ err: "invalid request" });
});
});
res.status(200).send({ msg: "success" });
} catch (error) {
logger.error(error);
res.status(500).send({ err: "invalid request" });

View File

@ -1,7 +1,6 @@
import express, { Request } from "express";
import passport from "passport";
const router = express.Router();
import axios from "axios";
import logger from "../logger/logger";
import Session from "../models/session";
@ -11,6 +10,7 @@ import {
PASSPORT_SUCCESS_REDIRECT_URL,
} from "../utils/constants";
import User from "../models/user";
import { terminPlanerRequest } from "../utils/terminPlaner";
router.get(
"/auth/google",
@ -57,28 +57,19 @@ router.get(
session_id: sessionId,
},
})
.then((userSession) => {
.then(async (userSession) => {
if (!userSession) {
logger.error("user session not found");
res.redirect(PASSPORT_FAILURE_REDIRECT_URL);
return;
}
axios
.post(
`${process.env.TERMIN_PLANNER_URL}/addGoogleAccount` as string,
{
userId: userSession.user_id,
accessToken: accessToken,
refreshToken: refreshToken,
sub: sub,
pass: process.env.TERMIN_PLANNER_AUTHORIZATION_PASSWORD as string,
}
)
.then(() => {})
.catch((err) => {
logger.info("err %s", err);
});
terminPlanerRequest("/api/v1/addGoogleAccount", "POST", {
userId: userSession.user_id,
accessToken: accessToken,
refreshToken: refreshToken,
sub: sub,
});
User.findOne({
where: {

View File

@ -81,3 +81,4 @@ export const GOOGLE_CALLBACK_URL = `${process.env.DASHBOARD_API_URL}/v1/calendar
export const PASSPORT_FAILURE_REDIRECT_URL = `${DASHBOARD_URL}/store/calendar/auth/failed`;
export const PASSPORT_SUCCESS_REDIRECT_URL = `${DASHBOARD_URL}/store/calendar/auth/finish`;
export const ACCOUNT_EXPORT_URL = `${DASHBOARD_URL}/v1/user/profile/export/`;
export const TERMIN_PLANNER_URL = process.env.TERMIN_PLANNER_URL;

View File

@ -1,19 +1,47 @@
import axios from "axios";
import logger from "../logger/logger";
import UserGoogleTokens from "../models/userGoogleTokens";
import { TERMIN_PLANNER_URL } from "./constants";
export function requestTerminPlanerChangeFutureBookingDays(storeId: string) {
axios
.post(
`${process.env.TERMIN_PLANNER_URL}/changeFutureBookingDays` as string,
{
storeId: storeId,
pass: process.env.TERMIN_PLANNER_AUTHORIZATION_PASSWORD as string,
}
)
.then((res) => {
logger.info("req planner max future booking days %s", res.data);
})
.catch((err) => {
logger.info("req planner max future booking days err %s", err);
});
export async function terminPlanerRequest(
url: string,
method: string,
body: any
) {
return await axios({
url: `${TERMIN_PLANNER_URL}${url}`,
method: method,
data: {
...body,
pass: process.env.TERMIN_PLANNER_AUTHORIZATION_PASSWORD as string,
},
});
}
// check if user has connected with google calendar
export async function isTerminPlanerGoogleCalendarConnected(
userId: string,
attributes?: string[]
) {
try {
const userGoogleToken = await UserGoogleTokens.findOne({
where: {
user_id: userId,
},
attributes: attributes,
});
return userGoogleToken;
} catch (error) {
logger.warn("isTerminPlanerGoogleCalendarConnected err:", error);
return null;
}
}
export async function terminPlanerRequestChangeFutureBookingDays(
storeId: string
) {
return await terminPlanerRequest("/changeFutureBookingDays", "post", {
storeId: storeId,
});
}