From 981cb9757db3f192a54d9941c4a2e9697af08571 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 11 Feb 2024 10:25:41 +0100 Subject: [PATCH] send request when creating or removing a employee --- src/controllers/calendarController.ts | 74 +++++++++++---------------- src/controllers/usersController.ts | 51 +++++++++++------- src/routes/calendarRoutes.ts | 25 +++------ src/utils/constants.ts | 1 + src/utils/terminPlaner.ts | 58 +++++++++++++++------ 5 files changed, 116 insertions(+), 93 deletions(-) diff --git a/src/controllers/calendarController.ts b/src/controllers/calendarController.ts index df0e8d1..b1c9bc1 100644 --- a/src/controllers/calendarController.ts +++ b/src/controllers/calendarController.ts @@ -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" }); diff --git a/src/controllers/usersController.ts b/src/controllers/usersController.ts index 6f871e7..d4af0f5 100644 --- a/src/controllers/usersController.ts +++ b/src/controllers/usersController.ts @@ -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" }); diff --git a/src/routes/calendarRoutes.ts b/src/routes/calendarRoutes.ts index 3b5239f..61443df 100644 --- a/src/routes/calendarRoutes.ts +++ b/src/routes/calendarRoutes.ts @@ -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: { diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 7b9246a..a174167 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -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; diff --git a/src/utils/terminPlaner.ts b/src/utils/terminPlaner.ts index fdce3e4..07efcc3 100644 --- a/src/utils/terminPlaner.ts +++ b/src/utils/terminPlaner.ts @@ -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, + }); }