Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

283 create api endpointserver func to delete allowed logins #345

Merged

Conversation

ethanhmaness
Copy link
Contributor

@ethanhmaness ethanhmaness commented Apr 24, 2024

Description

Added removeAllowedEmail() in src/server/actions/Setting.ts which removes an email from the allowed emails list in the settings document and deletes a user if there is a user associated with that email.
Added API endpoint at DELETE /api/settings/emails and associated zod types to call removeAllowedEmail().

Relevant issue(s)

#283

Questions

Should this include authorization?
Do we want to move this to Settings.ts?

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • I have assigned reviewers to this PR

To test

Log in as an admin and hit DELETE /api/settings/emails?email=<email to delete>.

@ethanhmaness ethanhmaness linked an issue Apr 24, 2024 that may be closed by this pull request
Copy link

vercel bot commented Apr 24, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
compassion-ministries ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 29, 2024 8:54pm

Copy link
Contributor

@zaviermiller zaviermiller left a comment

Choose a reason for hiding this comment

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

quick fix

Comment on lines 10 to 30
try {
dbConnect();
doc = await getSettings()
} catch (error) {
throw new CMError(CMErrorType.InternalError)
}

const index = doc.allowedEmails.indexOf(email)
// No matching email found in email document
if (index == -1) {
throw new CMError(CMErrorType.NoSuchKey, "Email")
} else {
// Modifies array in-place
doc.allowedEmails.splice(index, 1)
// Set database obj array to modified array of retrieved document
await SettingsSchema.updateOne({ _id: doc._id }, { allowedEmails: doc.allowedEmails })

// Delete associated user if one exists
try {
await UserSchema.findOneAndRemove({ email: email })
} catch (e) { }
Copy link
Contributor

Choose a reason for hiding this comment

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

why are there two try catches here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

First try catch is for throwing error if database call fails. Second try-catch never throws an error, it's just necessary to delete a user with the given email if one exists and to do nothing if one doesn't exist.

@ethanhmaness ethanhmaness merged commit 74d7b05 into main Apr 30, 2024
3 checks passed
@ethanhmaness ethanhmaness deleted the 283-create-api-endpointserver-func-to-delete-allowed-logins branch April 30, 2024 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create API endpoint/server func to delete allowed logins
2 participants