fix: reviewed-user-sms-lock (#16016)

* throw trpc error on re-review for sms

* update logic

* test

* update logic

---------

Co-authored-by: Amit Sharma <[email protected]>
This commit is contained in:
Natnael YilmaandAmit Sharma authored and GitHub committed 2024-08-12 08:12:48 +02:00
1 parent 829dd937d6
commit ad62f9d2bd
4 files changed
+49 -36

No files matched your search

@@ -55,6 +55,9 @@ async function changeSMSLockState(identifier: string, status: SMSLockState) {
}
if (userId) {
const user = await prisma.user.findUnique({ where: { id: userId, profiles: { none: {} } } });
if (user?.smsLockReviewedByAdmin) return;
await prisma.user.update({
where: {
id: userId,
@@ -65,6 +68,11 @@ async function changeSMSLockState(identifier: string, status: SMSLockState) {
},
});
} else {
const team = await prisma.team.findUnique({
where: { id: teamId, parentId: null, isOrganization: false },
});
if (team?.smsLockReviewedByAdmin) return;
await prisma.team.update({
where: {
id: teamId,
@@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "Team" ADD COLUMN "smsLockReviewedByAdmin" BOOLEAN NOT NULL DEFAULT false;
-- AlterTable
ALTER TABLE "users" ADD COLUMN "smsLockReviewedByAdmin" BOOLEAN NOT NULL DEFAULT false;
+4 -2
View File
@@ -320,8 +320,9 @@ model User {
secondaryEmails SecondaryEmail[]
isPlatformManaged Boolean @default(false)
OutOfOfficeReasons OutOfOfficeReason[]
smsLockState SMSLockState @default(UNLOCKED)
OutOfOfficeReasons OutOfOfficeReason[]
smsLockState SMSLockState @default(UNLOCKED)
smsLockReviewedByAdmin Boolean @default(false)
@@unique([email])
@@unique([email, username])
@@ -412,6 +413,7 @@ model Team {
smsLockState SMSLockState @default(UNLOCKED)
platformBilling PlatformBilling?
activeOrgWorkflows WorkflowsOnTeams[]
smsLockReviewedByAdmin Boolean @default(false)
@@unique([slug, parentId])
@@index([parentId])
@@ -16,17 +16,17 @@ type GetOptions = {
const setSMSLockState = async ({ input }: GetOptions) => {
const { userId, username, teamId, teamSlug, lock } = input;
if (userId) {
const userToUpdate = await prisma.user.findUnique({ where: { id: userId } });
if (!userToUpdate) throw new TRPCError({ code: "BAD_REQUEST", message: "User not found" });
const updatedUser = await prisma.user.update({
where: {
id: userId,
},
data: {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
smsLockReviewedByAdmin: true,
},
});
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({
@@ -35,32 +35,33 @@ const setSMSLockState = async ({ input }: GetOptions) => {
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 };
}
if (!userToUpdate) throw new TRPCError({ code: "BAD_REQUEST", message: "User not found" });
const updatedUser = await prisma.user.update({
where: {
id: userToUpdate.id,
},
data: {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
smsLockReviewedByAdmin: true,
},
});
return { name: updatedUser.username, locked: lock };
} else if (teamId) {
const teamToUpdate = await prisma.team.findUnique({
where: {
id: teamId,
},
});
if (!teamToUpdate) throw new TRPCError({ code: "BAD_REQUEST", message: "Team not found" });
const updatedTeam = await prisma.team.update({
where: {
id: teamId,
},
data: {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
smsLockReviewedByAdmin: true,
},
});
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({
@@ -69,20 +70,17 @@ const setSMSLockState = async ({ input }: GetOptions) => {
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 };
}
if (!teamToUpdate) throw new TRPCError({ code: "BAD_REQUEST", message: "Team not found" });
const updatedTeam = await prisma.team.update({
where: {
id: teamToUpdate.id,
},
data: {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
smsLockReviewedByAdmin: true,
},
});
return { name: updatedTeam.slug, locked: lock };
}
throw new TRPCError({ code: "BAD_REQUEST", message: "Input data missing" });
};