Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/app/api/attachments/attachments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import APIError from '@api/core/exceptions/api'
import httpStatus from 'http-status'
import { SupabaseService } from '@api/core/services/supabase.service'
import { signedUrlTtl } from '@/constants/attachments'
import { PrismaClient } from '@prisma/client'

export class AttachmentsService extends BaseService {
async getAttachments(taskId: string) {
Expand Down Expand Up @@ -91,13 +92,25 @@ export class AttachmentsService extends BaseService {
const policyGate = new PoliciesService(this.user)
policyGate.authorize(UserAction.Delete, Resource.Attachments)

const commentAttachment = await this.db.attachment.findMany({
where: { commentId: commentId, workspaceId: this.user.workspaceId },
})
await this.db.attachment.deleteMany({
where: { commentId: commentId, workspaceId: this.user.workspaceId },
const commentAttachment = await this.db.$transaction(async (tx) => {
this.setTransaction(tx as PrismaClient)

const commentAttachment = await this.db.attachment.findMany({
where: { commentId: commentId, workspaceId: this.user.workspaceId },
})

await this.db.attachment.deleteMany({
where: { commentId: commentId, workspaceId: this.user.workspaceId },
})

this.unsetTransaction()
return commentAttachment
})

return commentAttachment
// directly delete attachments from bucket when deleting comments.
// Postgres transaction is not valid for supabase object so placing it after record deletion from db
const filePathArray = commentAttachment.map((el) => el.filePath)
const supabase = new SupabaseService()
await supabase.removeAttachmentsFromBucket(filePathArray)
}
}
21 changes: 6 additions & 15 deletions src/app/api/comment/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,12 @@ export class CommentService extends BaseService {
const commentExists = await this.db.comment.findFirst({ where: { id } })
if (!commentExists) throw new APIError(httpStatus.NOT_FOUND, 'The comment to delete was not found')

// transaction that deletes the comment and its attachments
const { comment, attachments } = await this.db.$transaction(async (tx) => {
this.setTransaction(tx as PrismaClient)
const comment = await this.db.comment.delete({ where: { id } })

// delete the related attachments as well
const attachmentService = new AttachmentsService(this.user)
attachmentService.setTransaction(tx as PrismaClient)

const attachments = await attachmentService.deleteAttachmentsOfComment(comment.id)
attachmentService.unsetTransaction()
// delete the comment
const comment = await this.db.comment.delete({ where: { id } })

this.unsetTransaction()
return { comment, attachments }
})
// delete the related attachments as well
const attachmentService = new AttachmentsService(this.user)
await attachmentService.deleteAttachmentsOfComment(comment.id)

// transaction that deletes the activity logs
return await this.db.$transaction(async (tx) => {
Expand Down Expand Up @@ -127,7 +118,7 @@ export class CommentService extends BaseService {
tasksService.unsetTransaction()

this.unsetTransaction()
return { ...comment, attachments }
return { ...comment, attachments: [] } // send empty attachments array
})
}

Expand Down
13 changes: 13 additions & 0 deletions src/app/api/core/services/supabase.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import APIError from '@/app/api/core/exceptions/api'
import { supabaseBucket } from '@/config'
import SupabaseClient from '@/lib/supabase'
import { type SupabaseClient as SupabaseJSClient } from '@supabase/supabase-js'
import httpStatus from 'http-status'

/**
* Base Service with access to supabase client
*/
export class SupabaseService {
public supabase: SupabaseJSClient = SupabaseClient.getInstance()

async removeAttachmentsFromBucket(attachmentsToDelete: string[]) {
if (attachmentsToDelete.length !== 0) {
const { error } = await this.supabase.storage.from(supabaseBucket).remove(attachmentsToDelete)
if (error) {
console.error(error)
throw new APIError(httpStatus.NOT_FOUND, 'unable to delete some date from supabase')
}
}
}
}
10 changes: 3 additions & 7 deletions src/app/api/workers/scrap-medias/scrap-medias.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,9 @@ export class ScrapMediaService {
console.error('Error processing scrap image', e)
}
}
if (scrapMediasToDeleteFromBucket.length !== 0) {
const { error } = await supabase.supabase.storage.from(supabaseBucket).remove(scrapMediasToDeleteFromBucket)
if (error) {
console.error(error)
throw new APIError(404, 'unable to delete some date from supabase')
}
}
// remove attachments from bucket
await supabase.removeAttachmentsFromBucket(scrapMediasToDeleteFromBucket)

if (scrapMediasToDelete.length !== 0) {
const idsToDelete = scrapMediasToDelete.map((id) => `'${id}'`).join(', ')
await db.$executeRawUnsafe(`
Expand Down
Loading