Skip to content

fix: reviewed-user-sms-lock#16016

Merged
CarinaWolli merged 5 commits intocalcom:mainfrom
TaduJR:fix-reviewed-user-sms-lock
Aug 12, 2024
Merged

fix: reviewed-user-sms-lock#16016
CarinaWolli merged 5 commits intocalcom:mainfrom
TaduJR:fix-reviewed-user-sms-lock

Conversation

@TaduJR
Copy link
Copy Markdown
Contributor

@TaduJR TaduJR commented Jul 31, 2024

What does this PR do?

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • I have added a Docs issue here if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

  • Are there environment variables that should be set? No
  • What are the minimal test data to have? User or Team and Admin
  • What is expected (happy path) to have (input and output)?
  • Any other important info that could help to test that PR

Checklist

  • I haven't commented my code, particularly in hard-to-understand areas

@vercel
Copy link
Copy Markdown

vercel Bot commented Jul 31, 2024

@TaduJR is attempting to deploy a commit to the cal Team on Vercel.

A member of the Team first needs to authorize it.

@graphite-app graphite-app Bot added the community Created by Linear-GitHub Sync label Jul 31, 2024
@graphite-app graphite-app Bot requested a review from a team July 31, 2024 22:44
@github-actions github-actions Bot added teams area: teams, round robin, collective, managed event-types 🐛 bug Something isn't working 🧹 Improvements Improvements to existing features. Mostly UX/UI labels Jul 31, 2024
@dosubot dosubot Bot added the admin label Jul 31, 2024
@graphite-app
Copy link
Copy Markdown

graphite-app Bot commented Jul 31, 2024

Graphite Automations

"Add community label" took an action on this PR • (07/31/24)

1 label was added to this PR based on Keith Williams's automation.

"Add consumer team as reviewer" took an action on this PR • (07/31/24)

1 reviewer was added to this PR based on Keith Williams's automation.

@TaduJR TaduJR changed the title fix: inital commit fix: reviewed-user-sms-lock Jul 31, 2024
Copy link
Copy Markdown
Member

@Amit91848 Amit91848 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @TaduJR , but I guess you are misunderstanding the issue. Your changes doesn't solve the issue.

https://github.com/calcom/cal.com/blob/89c08868f6c069b1d8ceb163e3d9b5e7c24d3879/packages/lib/checkRateLimitAndThrowError.ts#L35-L46

This is where accounts are flagged and set for review. What we want is if an account was previously marked as reviewed and later unlocked by an admin, we do not want to mark it for review again

@@ -16,6 +16,20 @@ type GetOptions = {
const setSMSLockState = async ({ input }: GetOptions) => {
Copy link
Copy Markdown
Member

@Amit91848 Amit91848 Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mutation is only called inside apps/web/pages/settings/admin/lockedSMS/lockedSMSView.tsx where admin can mark the user or team as LOCKED or UNLOCKED only. These changes aren't required

Comment on lines +19 to +32
const userToUpdate = await prisma.user.findUnique({
where: {
id: userId,
},
});
if (!userToUpdate) {
throw new TRPCError({ code: "BAD_REQUEST", message: "User not found" });
}
if (userToUpdate.smsLockState === SMSLockState.UNLOCKED) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Re-review for unlocked user or team is not allowed",
});
}
Copy link
Copy Markdown
Contributor

@anikdhabal anikdhabal Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this is not correct, you checked whether the user's smslockstate is unlocked or not, but all users smslockstate UNLOCKED by default in the database.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@TaduJR
Copy link
Copy Markdown
Contributor Author

TaduJR commented Aug 2, 2024

Hello @Amit91848, and @anikdhabal. Thank you so much for your comments and review. Now I fully understand the requirement. I will work on it.

@TaduJR TaduJR force-pushed the fix-reviewed-user-sms-lock branch from 875fbb5 to 0c4ef91 Compare August 3, 2024 08:58
@github-actions github-actions Bot added the ❗️ migrations contains migration files label Aug 3, 2024
@TaduJR
Copy link
Copy Markdown
Contributor Author

TaduJR commented Aug 3, 2024

Hello @Amit91848. I have made a new push. But I have one question? As you can see I am throwing a trpc error for re-review sms. I think the trpc error is shown as toast on the frontend. Should continue on with the current approach?

Copy link
Copy Markdown
Member

@Amit91848 Amit91848 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @Amit91848. I have made a new push. But I have one question? As you can see I am throwing a trpc error for re-review sms. I think the trpc error is shown as toast on the frontend. Should continue on with the current approach?

Hey @TaduJR , this is close but we don't want to throw errors as it will halt the process. If admin has reviewed and unlocked the team or user, that means it is safe and does not need to be reviewed or locked again.

Comment thread packages/prisma/schema.prisma Outdated
smsLockState SMSLockState @default(UNLOCKED)
OutOfOfficeReasons OutOfOfficeReason[]
smsLockState SMSLockState @default(UNLOCKED)
isSMSLockStateReviewedBefore Boolean @default(false)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Better variable naming

Suggested change
isSMSLockStateReviewedBefore Boolean @default(false)
reviewedByAdmin Boolean @default(false)

Comment thread packages/lib/checkRateLimitAndThrowError.ts
@TaduJR
Copy link
Copy Markdown
Contributor Author

TaduJR commented Aug 4, 2024

Thank you so much @Amit91848.

@TaduJR TaduJR requested a review from Amit91848 August 4, 2024 19:03
Copy link
Copy Markdown
Member

@Amit91848 Amit91848 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some suggestions. Please make changes accordingly @TaduJR


if (userId) {
const user = await prisma.user.findUnique({ where: { id: userId, profiles: { none: {} } } });
if (status === SMSLockState.REVIEW_NEEDED && user?.smsLockReviewedByAdmin) return;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (status === SMSLockState.REVIEW_NEEDED && user?.smsLockReviewedByAdmin) return;
if (status === SMSLockState.UNLOCKED && user?.smsLockReviewedByAdmin) return;

If it was unlocked by admin then we don't need to mark it for review or locked again

Copy link
Copy Markdown
Contributor Author

@TaduJR TaduJR Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one needs to be if (user?.smsLockReviewedByAdmin) return; by removing SMSLockState.UNLOCKED condition because as you can see on changeSMSRateLimit function, the status (rate limiting type) is either LOCKED or REVIEW_NEED so no need to check for UNLOCKED.

const team = await prisma.team.findUnique({
where: { id: teamId, parentId: null, isOrganization: false },
});
if (status === SMSLockState.REVIEW_NEEDED && team?.smsLockReviewedByAdmin) return;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (status === SMSLockState.REVIEW_NEEDED && team?.smsLockReviewedByAdmin) return;
if (status === SMSLockState.UNLOCKED && team?.smsLockReviewedByAdmin) return;

Same here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this also

Comment on lines +20 to +26
if (!userToUpdate) throw new TRPCError({ code: "BAD_REQUEST", message: "User not found" });
const userUpdateInput = {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
...(userToUpdate.smsLockState === SMSLockState.REVIEW_NEEDED && !lock
? { smsLockReviewedByAdmin: true }
: {}),
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary. setSMSLockState is a privileged endpoint only accessible by admin, so you just have to update the reviewedByAdmin to true.

smsLockReviewedByAdmin means the LockState whether Reviewe_Needed, unlocked locked etc is being done by admin, so we will just mark it true no matter the state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

data: {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
},
data: userUpdateInput,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data: userUpdateInput,
data: {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
smsLockReviewedByAdmin: true,
},

setSMSLockState is a privileged endpoint only accessible by admin, so you just have to update the reviewedByAdmin to true.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines +62 to 74
if (!teamToUpdate) throw new TRPCError({ code: "BAD_REQUEST", message: "Team not found" });
const teamUpdateInput = {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
...(teamToUpdate.smsLockState === SMSLockState.REVIEW_NEEDED && !lock
? { smsLockReviewedByAdmin: true }
: {}),
};
const updatedTeam = await prisma.team.update({
where: {
id: teamId,
},
data: teamUpdateInput,
});
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines +83 to +96
if (!teamToUpdate) throw new TRPCError({ code: "BAD_REQUEST", message: "Team not found" });
const teamUpdateInput = {
smsLockState: lock ? SMSLockState.LOCKED : SMSLockState.UNLOCKED,
...(teamToUpdate.smsLockState === SMSLockState.REVIEW_NEEDED && !lock
? { smsLockReviewedByAdmin: true }
: {}),
};
const updatedTeam = await prisma.team.update({
where: {
id: teamToUpdate.id,
},
data: teamUpdateInput,
});
return { name: updatedTeam.slug, locked: lock };
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@TaduJR TaduJR requested review from Amit91848 and anikdhabal August 5, 2024 10:54
Copy link
Copy Markdown
Member

@Amit91848 Amit91848 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Tested and works 🚀
Thanks @TaduJR

@TaduJR
Copy link
Copy Markdown
Contributor Author

TaduJR commented Aug 12, 2024

My pleasure @Amit91848.

@CarinaWolli CarinaWolli merged commit ad62f9d into calcom:main Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

admin 🐛 bug Something isn't working community Created by Linear-GitHub Sync 🧹 Improvements Improvements to existing features. Mostly UX/UI ❗️ migrations contains migration files ready-for-e2e teams area: teams, round robin, collective, managed event-types

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CAL-4077] Already reviewed users/teams shouldn't be marked for review again (SMS sending)

4 participants