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 Store from "../models/store";
import { decodeBase64, getUserSession, matchPassword } from "../utils/utils"; import { decodeBase64, getUserSession, matchPassword } from "../utils/utils";
import User from "../models/user"; import User from "../models/user";
import UserGoogleTokens from "../models/userGoogleTokens";
import axios from "axios";
import { isPasswordValid } from "../validator/validator"; 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 // 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) { 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 // check if the user has the google calendar connected
const userGoogleToken = await UserGoogleTokens.findOne({ const googleCalendarConnected = await isTerminPlanerGoogleCalendarConnected(
where: { userSession.user_id,
user_id: userSession.user_id, ["status"]
}, );
attributes: ["status"],
});
if (!userGoogleToken) { if (!googleCalendarConnected) {
return res.status(200).send({ return res.status(200).send({
status: CalendarStatus.NOT_CONNECTED, status: CalendarStatus.NOT_CONNECTED,
}); });
} }
if (userGoogleToken.status !== CalendarStatus.OK) { if (googleCalendarConnected.status !== CalendarStatus.OK) {
return res.status(200).send({ 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) { if (store.owner_user_id !== userSession.user_id) {
// user is not store owner - means he is an employee // user is not store owner - means he is an employee
res.status(200).send({ res.status(200).send({
status: userGoogleToken.status, status: googleCalendarConnected.status,
userSettings: userSettings, userSettings: userSettings,
}); });
return; return;
} }
// user is store owner // user is store owner
res.status(200).send({ res.status(200).send({
status: userGoogleToken.status, status: googleCalendarConnected.status,
storeSettings: { storeSettings: {
calendar_max_future_booking_days: calendar_max_future_booking_days:
store.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 // check if the user has the google calendar connected
const userGoogleToken = await UserGoogleTokens.findOne({ const googleCalendarConnected = await isTerminPlanerGoogleCalendarConnected(
where: { userSession.user_id
user_id: userSession.user_id, );
},
});
if (!userGoogleToken) { if (!googleCalendarConnected) {
return res.status(401).send({ err: "unauthorized" }); 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 // check if the user has the google calendar connected
const userGoogleToken = await UserGoogleTokens.findOne({ const googleCalendarConnected = await isTerminPlanerGoogleCalendarConnected(
where: { userSession.user_id
user_id: userSession.user_id, );
},
});
if (!userGoogleToken) { if (!googleCalendarConnected) {
return res.status(401).send({ err: "unauthorized" }); 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, calendar_min_earliest_booking_time: calendarMinEarliestBookingTime,
}; };
requestTerminPlanerChangeFutureBookingDays(store.store_id); terminPlanerRequestChangeFutureBookingDays(store.store_id);
} }
if (Object.keys(update).length === 0) { 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 // check if the user has the google calendar connected
const userGoogleToken = await UserGoogleTokens.findOne({ const googleCalendarConnected = await isTerminPlanerGoogleCalendarConnected(
where: { userSession.user_id
user_id: userSession.user_id, );
},
});
if (!userGoogleToken) { if (!googleCalendarConnected) {
return res.status(401).send({ err: "unauthorized" }); 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 // request to termin planner to remove google account
axios await terminPlanerRequest("/api/v1/removeGoogleAccount", "POST", {
.post(`${process.env.TERMIN_PLANNER_URL}/removeGoogleAccount` as string, { userId: userSession.user_id,
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);
res.status(500).send({ err: "invalid request" }); res.status(200).send({ msg: "success" });
});
} catch (error) { } catch (error) {
logger.error(error); logger.error(error);
res.status(500).send({ err: "invalid request" }); res.status(500).send({ err: "invalid request" });

View File

@ -20,7 +20,11 @@ import {
USER_ANALYTICS_ENABLED_DEFAULT, USER_ANALYTICS_ENABLED_DEFAULT,
} from "../utils/constants"; } from "../utils/constants";
import Store from "../models/store"; 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) { export async function AddEmployee(req: Request, res: Response) {
try { try {
@ -141,12 +145,18 @@ export async function AddEmployee(req: Request, res: Response) {
}; };
} }
await User.create(newUser) const googleCalendarConnected = await isTerminPlanerGoogleCalendarConnected(
.then(() => res.status(200).send({ msg: "success" })) store.owner_user_id
.catch((err) => { );
logger.error(err);
res.status(500).send({ err: "invalid request" }); // 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) { } catch (error) {
logger.error(error); logger.error(error);
res.status(500).send({ err: "invalid request" }); 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, calendar_min_earliest_booking_time: calendarMinEarliestBookingTime,
}; };
requestTerminPlanerChangeFutureBookingDays(store.store_id); terminPlanerRequestChangeFutureBookingDays(store.store_id);
} }
if (Object.keys(update).length === 0) { 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" }); 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: { where: {
user_id: userId, user_id: userId,
}, },
}) });
.then(() => {
// delete user res.status(200).send({ msg: "success" });
res.status(200).send({ msg: "success" });
})
.catch((err) => {
logger.error(err);
res.status(500).send({ err: "invalid request" });
});
} catch (error) { } catch (error) {
logger.error(error); logger.error(error);
res.status(500).send({ err: "invalid request" }); res.status(500).send({ err: "invalid request" });

View File

@ -1,7 +1,6 @@
import express, { Request } from "express"; import express, { Request } from "express";
import passport from "passport"; import passport from "passport";
const router = express.Router(); const router = express.Router();
import axios from "axios";
import logger from "../logger/logger"; import logger from "../logger/logger";
import Session from "../models/session"; import Session from "../models/session";
@ -11,6 +10,7 @@ import {
PASSPORT_SUCCESS_REDIRECT_URL, PASSPORT_SUCCESS_REDIRECT_URL,
} from "../utils/constants"; } from "../utils/constants";
import User from "../models/user"; import User from "../models/user";
import { terminPlanerRequest } from "../utils/terminPlaner";
router.get( router.get(
"/auth/google", "/auth/google",
@ -57,28 +57,19 @@ router.get(
session_id: sessionId, session_id: sessionId,
}, },
}) })
.then((userSession) => { .then(async (userSession) => {
if (!userSession) { if (!userSession) {
logger.error("user session not found"); logger.error("user session not found");
res.redirect(PASSPORT_FAILURE_REDIRECT_URL); res.redirect(PASSPORT_FAILURE_REDIRECT_URL);
return; return;
} }
axios terminPlanerRequest("/api/v1/addGoogleAccount", "POST", {
.post( userId: userSession.user_id,
`${process.env.TERMIN_PLANNER_URL}/addGoogleAccount` as string, accessToken: accessToken,
{ refreshToken: refreshToken,
userId: userSession.user_id, sub: sub,
accessToken: accessToken, });
refreshToken: refreshToken,
sub: sub,
pass: process.env.TERMIN_PLANNER_AUTHORIZATION_PASSWORD as string,
}
)
.then(() => {})
.catch((err) => {
logger.info("err %s", err);
});
User.findOne({ User.findOne({
where: { 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_FAILURE_REDIRECT_URL = `${DASHBOARD_URL}/store/calendar/auth/failed`;
export const PASSPORT_SUCCESS_REDIRECT_URL = `${DASHBOARD_URL}/store/calendar/auth/finish`; 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 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 axios from "axios";
import logger from "../logger/logger"; import logger from "../logger/logger";
import UserGoogleTokens from "../models/userGoogleTokens";
import { TERMIN_PLANNER_URL } from "./constants";
export function requestTerminPlanerChangeFutureBookingDays(storeId: string) { export async function terminPlanerRequest(
axios url: string,
.post( method: string,
`${process.env.TERMIN_PLANNER_URL}/changeFutureBookingDays` as string, body: any
{ ) {
storeId: storeId, return await axios({
pass: process.env.TERMIN_PLANNER_AUTHORIZATION_PASSWORD as string, url: `${TERMIN_PLANNER_URL}${url}`,
} method: method,
) data: {
.then((res) => { ...body,
logger.info("req planner max future booking days %s", res.data); pass: process.env.TERMIN_PLANNER_AUTHORIZATION_PASSWORD as string,
}) },
.catch((err) => { });
logger.info("req planner max future booking days err %s", err); }
});
// 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,
});
} }