From e8f2c691830b9f335dc7c1a2023f94fda591b637 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 20 Jan 2024 00:35:37 +0100 Subject: [PATCH] calendar not connected --- src/controllers/calendarController.ts | 39 +++++++++++++++ src/models/index.ts | 2 + src/models/userGoogleTokens.ts | 72 +++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 src/models/userGoogleTokens.ts diff --git a/src/controllers/calendarController.ts b/src/controllers/calendarController.ts index d2fa448..3923673 100644 --- a/src/controllers/calendarController.ts +++ b/src/controllers/calendarController.ts @@ -3,6 +3,7 @@ import logger from "../logger/logger"; import Store from "../models/store"; import { getUserSession } from "../utils/utils"; import User from "../models/user"; +import UserGoogleTokens from "../models/userGoogleTokens"; // 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) { @@ -41,6 +42,20 @@ export async function GetCalendarSettings(req: Request, res: Response) { return res.status(401).send({ err: "unauthorized" }); } + // check if the user has the google calendar connected + + const userGoogleToken = await UserGoogleTokens.findOne({ + where: { + user_id: userSession.user_id, + }, + }); + + if (!userGoogleToken) { + return res.status(200).send({ + connected: false, + }); + } + // get user const user = await User.findOne({ @@ -105,6 +120,18 @@ export async function UpdatePersonalCalendarSettings( return res.status(401).send({ err: "unauthorized" }); } + // check if the user has the google calendar connected + + const userGoogleToken = await UserGoogleTokens.findOne({ + where: { + user_id: userSession.user_id, + }, + }); + + if (!userGoogleToken) { + return res.status(401).send({ err: "unauthorized" }); + } + // update user await User.update( @@ -145,6 +172,18 @@ export async function UpdateStoreCalendarSettings(req: Request, res: Response) { return res.status(401).send({ err: "unauthorized" }); } + // check if the user has the google calendar connected + + const userGoogleToken = await UserGoogleTokens.findOne({ + where: { + user_id: userSession.user_id, + }, + }); + + if (!userGoogleToken) { + return res.status(401).send({ err: "unauthorized" }); + } + // verify if requester is a store master const store = await Store.findOne({ diff --git a/src/models/index.ts b/src/models/index.ts index 0dc271c..222c122 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -13,6 +13,8 @@ function syncModels() { StoreService.sync(); StoreServiceActivity.sync(); StoreServiceActivityUsers.sync(); + + // UserGoogleTokens.sync(); not needed as it is created by the calendar backend } export default syncModels; diff --git a/src/models/userGoogleTokens.ts b/src/models/userGoogleTokens.ts new file mode 100644 index 0000000..51e07ae --- /dev/null +++ b/src/models/userGoogleTokens.ts @@ -0,0 +1,72 @@ +import { DataTypes, Model } from "sequelize"; +import sequelize from "../database/database"; + +/* + initial created by the calendar backend +*/ + +interface UserGoogleTokensAttributes { + user_id: string; + access_token?: string; + refresh_token?: string; + expiry_date?: Date; + google_uuid?: string; + created_at?: Date; + updated_at?: Date; +} + +class UserGoogleTokens + extends Model + implements UserGoogleTokensAttributes +{ + declare user_id: string; + declare access_token: string; + declare refresh_token: string; + declare expiry_date: Date; + declare google_uuid: string; + declare created_at: Date; + declare updated_at: Date; +} + +UserGoogleTokens.init( + { + // Model attributes are defined here + user_id: { + primaryKey: true, + type: DataTypes.STRING, + allowNull: false, + }, + access_token: { + type: DataTypes.STRING, + // allowNull defaults to true + }, + refresh_token: { + type: DataTypes.STRING, + // allowNull defaults to true + }, + expiry_date: { + type: DataTypes.DATE, + // allowNull defaults to true + }, + google_uuid: { + type: DataTypes.STRING, + // allowNull defaults to true + }, + created_at: { + type: DataTypes.DATE, + // allowNull defaults to true + }, + updated_at: { + type: DataTypes.DATE, + // allowNull defaults to true + }, + }, + { + tableName: "user_google_tokens", + sequelize, // passing the `sequelize` instance is required + createdAt: "created_at", + updatedAt: "updated_at", + } +); + +export default UserGoogleTokens;