feat: invitation deletion
This commit is contained in:
@@ -75,6 +75,69 @@ export async function createInvitationWithMembers(input: CreateInvitationInput)
|
||||
})
|
||||
}
|
||||
|
||||
export interface DeletedInvitationResult {
|
||||
id: string
|
||||
displayName: string
|
||||
photoObjectKey: string | null
|
||||
}
|
||||
|
||||
export async function deleteInvitationGroup(
|
||||
invitationId: string,
|
||||
adminEmail: string,
|
||||
): Promise<DeletedInvitationResult | null> {
|
||||
return db.transaction().execute(async (trx) => {
|
||||
const invitation = await trx
|
||||
.selectFrom("invitations")
|
||||
.select(["id", "display_name", "token"])
|
||||
.where("id", "=", invitationId)
|
||||
.executeTakeFirst()
|
||||
|
||||
if (!invitation) return null
|
||||
|
||||
const photoUpload = await trx
|
||||
.selectFrom("photo_uploads")
|
||||
.select(["object_key"])
|
||||
.where("invitation_id", "=", invitationId)
|
||||
.executeTakeFirst()
|
||||
|
||||
const membersCount = await trx
|
||||
.selectFrom("invitation_members")
|
||||
.select((eb) => eb.fn.countAll<string>().as("count"))
|
||||
.where("invitation_id", "=", invitationId)
|
||||
.executeTakeFirst()
|
||||
|
||||
await trx
|
||||
.insertInto("admin_audit_events")
|
||||
.values({
|
||||
admin_email: adminEmail,
|
||||
action: "invitation.deleted",
|
||||
target_type: "invitation",
|
||||
target_id: invitationId,
|
||||
metadata: {
|
||||
displayName: invitation.display_name,
|
||||
token: invitation.token,
|
||||
membersCount: Number(membersCount?.count ?? 0),
|
||||
photoObjectKey: photoUpload?.object_key ?? null,
|
||||
},
|
||||
})
|
||||
.execute()
|
||||
|
||||
const deleted = await trx
|
||||
.deleteFrom("invitations")
|
||||
.where("id", "=", invitationId)
|
||||
.returning(["id", "display_name"])
|
||||
.executeTakeFirst()
|
||||
|
||||
if (!deleted) return null
|
||||
|
||||
return {
|
||||
id: deleted.id,
|
||||
displayName: deleted.display_name,
|
||||
photoObjectKey: photoUpload?.object_key ?? null,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export async function listInvitations(): Promise<InvitationListItem[]> {
|
||||
const invitations = await db
|
||||
.selectFrom("invitations")
|
||||
|
||||
@@ -45,4 +45,9 @@ export const createInvitationSchema = z
|
||||
path: ["members"],
|
||||
})
|
||||
|
||||
export const deleteInvitationSchema = z.object({
|
||||
invitationId: z.uuid(),
|
||||
})
|
||||
|
||||
export type CreateInvitationInput = z.infer<typeof createInvitationSchema>
|
||||
export type DeleteInvitationInput = z.infer<typeof deleteInvitationSchema>
|
||||
|
||||
Reference in New Issue
Block a user