calendar not connected

main
alex 2024-01-20 00:35:37 +01:00
parent 927a998060
commit e8f2c69183
3 changed files with 113 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import logger from "../logger/logger";
import Store from "../models/store"; import Store from "../models/store";
import { getUserSession } from "../utils/utils"; import { getUserSession } from "../utils/utils";
import User from "../models/user"; 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 // 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) {
@ -41,6 +42,20 @@ export async function GetCalendarSettings(req: Request, res: Response) {
return res.status(401).send({ err: "unauthorized" }); 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 // get user
const user = await User.findOne({ const user = await User.findOne({
@ -105,6 +120,18 @@ export async function UpdatePersonalCalendarSettings(
return res.status(401).send({ err: "unauthorized" }); 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 // update user
await User.update( await User.update(
@ -145,6 +172,18 @@ export async function UpdateStoreCalendarSettings(req: Request, res: Response) {
return res.status(401).send({ err: "unauthorized" }); 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 // verify if requester is a store master
const store = await Store.findOne({ const store = await Store.findOne({

View File

@ -13,6 +13,8 @@ function syncModels() {
StoreService.sync(); StoreService.sync();
StoreServiceActivity.sync(); StoreServiceActivity.sync();
StoreServiceActivityUsers.sync(); StoreServiceActivityUsers.sync();
// UserGoogleTokens.sync(); not needed as it is created by the calendar backend
} }
export default syncModels; export default syncModels;

View File

@ -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<UserGoogleTokensAttributes>
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;