feat: orgs trigger alert when loading a calendar and no availability is (#14796)

* Install upstash redis again - base of noficationSender

* Setup email handler etc and use tasker

* Remove logs

* Sending emails and correct format and spacing in emails

* Update email styles

* Update email styles

* add switch to enable feature

* fix: reset tasker types - will fix in new PR

* WIP: test suite

* WIP: test suite

* Add redis service and add WIP mock test

* Add tests for redis service and fix lpush

* More working tests

* More working tests

* fix: type error + typo

* update export to match i18n next mock

* (debug) push for debug

* Fix: fixed hosting in mocks

* Add common.json i18n

* add better test descriptions

* reset mocks after each test to allow concurancy

* Remove redundant RedisService.test.ts

* Rename and remove redundant isAdmin check

* Rename to .d.ts

* Add key versioning to constructRedisKey

* Update packages/trpc/server/routers/viewer/slots/handleNotificationWhenNoSlots.ts

Co-authored-by: Hariom Balhara <[email protected]>

* Update packages/trpc/server/routers/viewer/slots/handleNotificationWhenNoSlots.ts

Co-authored-by: Hariom Balhara <[email protected]>

* Add try/catch

* fix breaking when option === undefined

* Add missing await to Promise.all

* Rename data stored in redis to save space

* Include thrown error in logs

---------

Co-authored-by: Hariom Balhara <[email protected]>
Co-authored-by: Joe Au-Yeung <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-05-03 13:48:04 +00:00
1 parent 88f4dd2446
commit c478f73327
19 files changed
+508 -10

No files matched your search

@@ -0,0 +1,52 @@
import { useState } from "react";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import type { RouterOutputs } from "@calcom/trpc";
import { trpc } from "@calcom/trpc";
import { SettingsToggle, showToast } from "@calcom/ui";
interface GeneralViewProps {
currentOrg: RouterOutputs["viewer"]["organizations"]["listCurrent"];
isAdminOrOwner: boolean;
}
export const NoSlotsNotificationSwitch = ({ currentOrg, isAdminOrOwner }: GeneralViewProps) => {
const { t } = useLocale();
const utils = trpc.useUtils();
const [notificationActive, setNotificationActive] = useState(
!!currentOrg.organizationSettings.adminGetsNoSlotsNotification
);
const mutation = trpc.viewer.organizations.update.useMutation({
onSuccess: async () => {
showToast(t("settings_updated_successfully"), "success");
},
onError: () => {
showToast(t("error_updating_settings"), "error");
},
onSettled: () => {
utils.viewer.organizations.listCurrent.invalidate();
},
});
if (!isAdminOrOwner) return null;
return (
<>
<SettingsToggle
toggleSwitchAtTheEnd={true}
title={t("organization_no_slots_notification_switch_title")}
disabled={mutation?.isPending}
description={t("organization_no_slots_notification_switch_description")}
checked={notificationActive}
onCheckedChange={(checked) => {
mutation.mutate({
adminGetsNoSlotsNotification: checked,
});
setNotificationActive(checked);
}}
switchContainerClassName="mt-6"
/>
</>
);
};
@@ -27,6 +27,7 @@ import {
} from "@calcom/ui";
import { LockEventTypeSwitch } from "../components/LockEventTypeSwitch";
import { NoSlotsNotificationSwitch } from "../components/NoSlotsNotificationSwitch";
const SkeletonLoader = ({ title, description }: { title: string; description: string }) => {
return (
@@ -87,6 +88,7 @@ const OrgGeneralView = () => {
/>
<LockEventTypeSwitch currentOrg={currentOrg} isAdminOrOwner={!!isAdminOrOwner} />
<NoSlotsNotificationSwitch currentOrg={currentOrg} isAdminOrOwner={!!isAdminOrOwner} />
</LicenseRequired>
);
};
+11
View File
@@ -0,0 +1,11 @@
export interface IRedisService {
get: <TData>(key: string) => Promise<TData | null>;
set: <TData>(key: string, value: TData) => Promise<"OK" | TData | null>;
expire: (key: string, seconds: number) => Promise<0 | 1>;
lrange: <TResult = string>(key: string, start: number, end: number) => Promise<TResult[]>;
lpush: <TData>(key: string, ...elements: TData[]) => Promise<number>;
}
+33
View File
@@ -0,0 +1,33 @@
import { Redis } from "@upstash/redis";
import type { IRedisService } from "./IRedisService";
export class RedisService implements IRedisService {
private redis: Redis;
constructor() {
this.redis = Redis.fromEnv();
}
async get<TData>(key: string): Promise<TData | null> {
return this.redis.get(key);
}
async set<TData>(key: string, value: TData): Promise<"OK" | TData | null> {
// Implementation for setting value in Redis
return this.redis.set(key, value);
}
async expire(key: string, seconds: number): Promise<0 | 1> {
// Implementation for setting expiration time for key in Redis
return this.redis.expire(key, seconds);
}
async lrange<TResult = string>(key: string, start: number, end: number): Promise<TResult[]> {
return this.redis.lrange(key, start, end);
}
async lpush<TData>(key: string, ...elements: TData[]): Promise<number> {
return this.redis.lpush(key, elements);
}
}