-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9232 from hicommonwealth/rotorsoft/9198-delete-co…
…mment Refactors delete comment
- Loading branch information
Showing
18 changed files
with
167 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { InvalidActor, type Command } from '@hicommonwealth/core'; | ||
import * as schemas from '@hicommonwealth/schemas'; | ||
import { models } from '../database'; | ||
import { isAuthorized, type AuthContext } from '../middleware'; | ||
import { mustBeAuthorized, mustExist } from '../middleware/guards'; | ||
|
||
export function DeleteComment(): Command< | ||
typeof schemas.DeleteComment, | ||
AuthContext | ||
> { | ||
return { | ||
...schemas.DeleteComment, | ||
auth: [isAuthorized({})], | ||
body: async ({ actor, payload, auth }) => { | ||
const { address } = mustBeAuthorized(actor, auth); | ||
const { comment_id, message_id } = payload; | ||
|
||
const comment = await models.Comment.findOne({ | ||
where: message_id | ||
? { discord_meta: { message_id } } | ||
: { id: comment_id }, | ||
include: [ | ||
{ | ||
model: models.Thread, | ||
attributes: ['community_id'], | ||
required: true, | ||
}, | ||
], | ||
}); | ||
mustExist('Comment', comment); | ||
|
||
if (comment.address_id !== address!.id && address.role === 'member') | ||
throw new InvalidActor(actor, 'Not authorized author'); | ||
|
||
// == mutation transaction boundary == | ||
await models.sequelize.transaction(async (transaction) => { | ||
await models.CommentSubscription.destroy({ | ||
where: { comment_id: comment.id }, | ||
transaction, | ||
}); | ||
await comment.destroy({ transaction }); | ||
}); | ||
// == end of transaction boundary == | ||
|
||
return { | ||
comment_id: comment.id!, | ||
canvas_signed_data: comment.canvas_signed_data, | ||
canvas_msg_id: comment.canvas_msg_id, | ||
}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './CreateComment.command'; | ||
export * from './CreateCommentReaction.command'; | ||
export * from './DeleteComment.command'; | ||
export * from './GetComments.query'; | ||
export * from './SearchComments.query'; | ||
export * from './UpdateComment.command'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.