Files
calendar/packages/trpc/server/routers/viewer/admin/setSMSLockState.handler.ts
T
883d745784 feat: SMS/Whatsapp to attendee for teams (#14648)
* remove event type requires confirmation

* allow sms sending also for team (frontend)

* allow text to attendee for team (backend)

* add rate limiting for SMS (WIP)

* add functionality to lock and unlock SMS sending (+admin UI view)

* rename to smsLockState

* add ability to lock users and teams

* don't send sms if locked

* add missing imports

* fix rate limit namespace

* add translation

* fix type error

* remove prop from UsersTable

* add smsLockState to buildUser

* code clean up

* create seperate sms rate limit function

* fix type error

* add missing userId and teamId to sendSMS

* code improvements

* add User Team type

* fix marking as reviewed

* check monthly sms limit only for user

* don't lock orgs

* fix type error

* fix avatars

* fix type error

* fix type error

* fix type error

---------

Co-authored-by: CarinaWolli <wollencarina@gmail.com>
Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
2024-04-23 19:48:44 +00:00

91 lines
2.5 KiB
TypeScript

import { prisma } from "@calcom/prisma";
import { SMSLockState } from "@calcom/prisma/client";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../trpc";
import type { TSetSMSLockState } from "./setSMSLockState.schema";
type GetOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TSetSMSLockState;
};
const setSMSLockState = async ({ input }: GetOptions) => {
const { userId, username, teamId, teamSlug, lock } = input;
if (userId) {
const updatedUser = await prisma.user.update({
where: {
id: userId,
},
data: {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
},
});
if (!updatedUser) {
throw new TRPCError({ code: "BAD_REQUEST", message: "User not found" });
}
return { name: updatedUser.username, locked: lock };
} else if (username) {
const userToUpdate = await prisma.user.findFirst({
where: {
username,
profiles: { none: {} },
},
});
if (!userToUpdate) {
throw new TRPCError({ code: "BAD_REQUEST", message: "User not found" });
}
if (userToUpdate) {
const updatedUser = await prisma.user.update({
where: {
id: userToUpdate.id,
},
data: {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
},
});
return { name: updatedUser.username, locked: lock };
}
} else if (teamId) {
const updatedTeam = await prisma.team.update({
where: {
id: teamId,
},
data: {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
},
});
if (!updatedTeam) {
throw new TRPCError({ code: "BAD_REQUEST", message: "Team not found" });
}
return { name: updatedTeam.slug, locked: lock };
} else if (teamSlug) {
const teamToUpdate = await prisma.team.findFirst({
where: {
slug: teamSlug,
parentId: null,
},
});
if (!teamToUpdate) {
throw new TRPCError({ code: "BAD_REQUEST", message: "Team not found" });
}
if (teamToUpdate) {
const updatedTeam = await prisma.team.update({
where: {
id: teamToUpdate.id,
},
data: {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
},
});
return { name: updatedTeam.slug, locked: lock };
}
}
throw new TRPCError({ code: "BAD_REQUEST", message: "Input data missing" });
};
export default setSMSLockState;