Files
calendar/packages/lib/sync/ISyncService.ts
T
Omar LópezandGitHub 7c749299bb Enforces explicit type imports (#7158)
* Enforces explicit type imports

* Upgrades typescript-eslint

* Upgrades eslint related dependencies

* Update config

* Sync packages mismatches

* Syncs prettier version

* Linting

* Relocks node version

* Fixes

* Locks @vitejs/plugin-react to 1.3.2

* Linting
2023-02-16 15:39:57 -07:00

94 lines
2.0 KiB
TypeScript

import type logger from "@calcom/lib/logger";
import { default as webPrisma } from "@calcom/prisma";
export type UserInfo = {
email: string;
name: string | null;
id: number;
username: string | null;
createdDate: Date;
};
export type TeamInfoType = {
name: string | undefined | null;
};
export type WebUserInfoType = UserInfo & {
/** All users are PRO now */
plan?: "PRO";
};
export type ConsoleUserInfoType = UserInfo & {
plan: "CLOUD" | "SELFHOSTED"; // DeploymentType;
};
export interface IUserDeletion<T> {
delete(info: T): Promise<any>;
}
export interface IUserCreation<T> {
create(info: T): Promise<any>;
update(info: T): Promise<any>;
upsert?: never;
}
export interface IUserUpsertion<T> {
create?: never;
update?: never;
upsert(info: T): Promise<any>;
}
export interface ISyncService {
ready(): boolean;
web: {
user: (IUserCreation<WebUserInfoType> | IUserUpsertion<WebUserInfoType>) & IUserDeletion<WebUserInfoType>;
};
console: {
user: IUserCreation<ConsoleUserInfoType> | IUserUpsertion<ConsoleUserInfoType>;
};
}
export default class SyncServiceCore {
protected serviceName: string;
protected service: any;
protected log: typeof logger;
constructor(serviceName: string, service: any, log: typeof logger) {
this.serviceName = serviceName;
this.log = log;
try {
this.service = new service();
} catch (e) {
this.log.warn("Couldn't instantiate sync service:", (e as Error).message);
}
}
ready() {
return this.service !== undefined;
}
async getUserLastBooking(user: { email: string }): Promise<{ booking: { createdAt: Date } | null } | null> {
return await webPrisma.attendee.findFirst({
where: {
email: user.email,
},
select: {
booking: {
select: {
createdAt: true,
},
},
},
orderBy: {
booking: {
createdAt: "desc",
},
},
});
}
}
export interface ISyncServices {
new (): ISyncService;
}