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

Ot.delete template #175

Merged
merged 8 commits into from
Jul 2, 2023
Merged

Ot.delete template #175

merged 8 commits into from
Jul 2, 2023

Conversation

CerberusLatrans
Copy link
Contributor

@CerberusLatrans CerberusLatrans commented May 28, 2023

Checklist

  • 1. Change ClickUp ticket status to "In Review"
  • 2. After making the PR, add PR link to ClickUp ticket

Why

ClickUp Ticket

Allows admins to delete existing email templates in the email-templates folder of the sftt-user-uploads bucket

This PR

First checks if the file exists with doesObjectExist; however instead of returning false if the file does not exist, it throws an exception (seems to come from trying to access metadata) for a 403 forbidden error. Tried to enable attribute permissions on the bucket but doesn't work. Might have to instantiate S3 client with credentials for it to work? Works fine (returns true) with the file does actually exist.

There might be a better workaround, but I catch the S3 exception and throw an InvaliURLException instead.

Verification Steps

Calling a POST request http://localhost:8081/api/v1/protected/emailer/delete_template/[some existing name].html
removes that file from the S3 folder

Copy link
Contributor

@huang0h huang0h left a comment

Choose a reason for hiding this comment

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

Overall LGTM, nice work! Most of my comments from the load template PR also apply here, specifically about the expected HTML path and adding AWS credentials.


externs.getS3Client().deleteObject(awsRequest);

return String.format("%s/%s/%s", externs.getBucketPublicUrl(), directoryName, name);
Copy link
Contributor

Choose a reason for hiding this comment

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

⛏️ Do we use the return value anywhere? If not, I think we can just remove it, and make the method void instead.

Copy link
Member

@chromium-52 chromium-52 left a comment

Choose a reason for hiding this comment

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

lgtm! Just a few small comments

@Override
public void deleteTemplate(JWTData userData, String templateName) {
assertAdminOrSuperAdmin(userData.getPrivilegeLevel());
S3Requester.deleteHTML(templateName, "email_templates");
Copy link
Member

Choose a reason for hiding this comment

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

⛏️ Can we use the TEMPLATE_DIR constant 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.

Yep, I forgot to change that once load template was merged

String HTMLPath = directoryName + "/" + name;

// if the object does not exist, it throws a 403 for metadata?
try {
Copy link
Member

Choose a reason for hiding this comment

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

🔧 Now that we're initializing s3Client with the access/secret keys, we might not need this try-catch

* @throws SdkClientException if the deletion from S3 failed.
*/
public static void deleteHTML(String name, String directoryName) {
String HTMLPath = directoryName + "/" + name;
Copy link
Member

@chromium-52 chromium-52 Jun 18, 2023

Choose a reason for hiding this comment

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

⛏️ Could we append _template.html at the end of htmlPath here so that the caller won't have to specify that (for instance, if someone wants to delete a template called some_name_template.html, then they would only have to pass in some_name as the name of the template to delete)? The fact that we append _template.html to the name of the template when we store it in S3 is an implementation detail and if possible, we want to hide it from the user so that they won't have to care about it to make their life easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point-- I forgot to add this for delete template like with load and add template.

Copy link
Contributor

@huang0h huang0h left a comment

Choose a reason for hiding this comment

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

Good work! Just a couple comments:

@@ -60,4 +61,18 @@ private void handleLoadTemplate(RoutingContext ctx) {

end(ctx.response(), 200, JsonObject.mapFrom(loadTemplateResponse).toString());
}

private void registerDeleteTemplate(Router router) {
Route deleteTemplate = router.post("/delete_template/:template_name");
Copy link
Contributor

Choose a reason for hiding this comment

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

⛏️ Small nitpick - can we use route.delete() here to register a delete route? This way the request we make will be a little more idiomatic and match what we're trying to do (if you're not familiar with http methods, MDN has good info about them)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure that makes sense; I was just copying the style of the other routes according to the API like delete site or delete user. Do you think it makes sense to refactor these too?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's definitely a good housekeeping thing to do, but not that important right now - if we change the backend routes, we'll need to make changes in the frontend as well.

Comment on lines 341 to 343

Boolean htmlExists = externs.getS3Client().doesObjectExist(externs.getBucketPublic(), HTMLPath);

Copy link
Contributor

Choose a reason for hiding this comment

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

🔧 Can we use the pathExists() method here as well? We should also throw an InvalidURLException if the file doesn't exist

Copy link
Contributor

@huang0h huang0h left a comment

Choose a reason for hiding this comment

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

lgtm!

@CerberusLatrans CerberusLatrans merged commit 4643db2 into master Jul 2, 2023
1 check passed
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 route for admins to delete existing email templates
3 participants