feat: Add workspace delete feature (#896)

* Add workspace delete feature

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Add fixes and refactors

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Add more fixes

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Add requested changes

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Add workspace delete mutation

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Complete v1 of deletion

Co-authored-by: Benjamin Mayanja <vibenjamin6@gmail.com>

* Revert unwanted changes

Co-authored-by: Benjamin Mayanja <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>

* Update debouce import

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>

* Fix server e2e tests on CI #3

* Fix server e2e tests on CI #4

* Fix server e2e tests on CI #5

* Added generic relation cell (#969)

* Added generic relation cell

* Deactivated debug

* Added default warning

* Put back display component

* Removed unused types

* fix: 906 edit avatar style (#923)

* fix: 906 edit avatar style

* fix: 906 add avatar size enum and mapping for font and height

* fix: 906 remove unused vars

* chore: optimize size of front docker image (#965)

* Enable to drag under New button on pipeline (#970)

* Add minor fix

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>

---------

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>
Co-authored-by: Charles Bochet <charles@twenty.com>
Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
Co-authored-by: 310387 <139059022+310387@users.noreply.github.com>
Co-authored-by: Lucas Vieira <vieiralucas4@gmail.com>
Co-authored-by: Charles Bochet <charlesBochet@users.noreply.github.com>
This commit is contained in:
gitstart-twenty
2023-07-28 12:19:20 +07:00
committed by GitHub
parent 8cf8183342
commit fcdde024a3
9 changed files with 303 additions and 18 deletions

View File

@ -20,7 +20,12 @@ import { FileUploadService } from 'src/core/file/services/file-upload.service';
import { streamToBuffer } from 'src/utils/stream-to-buffer';
import { AbilityGuard } from 'src/guards/ability.guard';
import { CheckAbilities } from 'src/decorators/check-abilities.decorator';
import { UpdateWorkspaceAbilityHandler } from 'src/ability/handlers/workspace.ability-handler';
import {
UpdateWorkspaceAbilityHandler,
DeleteWorkspaceAbilityHandler,
} from 'src/ability/handlers/workspace.ability-handler';
import { AuthUser } from 'src/decorators/auth-user.decorator';
import { User } from 'src/core/@generated/user/user.model';
@UseGuards(JwtAuthGuard)
@Resolver(() => Workspace)
@ -95,4 +100,20 @@ export class WorkspaceResolver {
return paths[0];
}
@UseGuards(AbilityGuard)
@CheckAbilities(DeleteWorkspaceAbilityHandler)
@Mutation(() => Workspace)
async deleteCurrentWorkspace(
@AuthWorkspace() { id: workspaceId }: Workspace,
@PrismaSelector({ modelName: 'Workspace' })
{ value: select }: PrismaSelect<'Workspace'>,
@AuthUser() { id: userId }: User,
) {
return this.workspaceService.deleteWorkspace({
workspaceId,
select,
userId,
});
}
}

View File

@ -6,6 +6,7 @@ import { PipelineService } from 'src/core/pipeline/services/pipeline.service';
import { PipelineStageService } from 'src/core/pipeline/services/pipeline-stage.service';
import { PersonService } from 'src/core/person/person.service';
import { CompanyService } from 'src/core/company/company.service';
import { PipelineProgressService } from 'src/core/pipeline/services/pipeline-progress.service';
import { WorkspaceService } from './workspace.service';
@ -36,6 +37,10 @@ describe('WorkspaceService', () => {
provide: CompanyService,
useValue: {},
},
{
provide: PipelineProgressService,
useValue: {},
},
],
}).compile();

View File

@ -1,12 +1,15 @@
import { Injectable } from '@nestjs/common';
import { v4 } from 'uuid';
import { Prisma } from '@prisma/client';
import { PipelineStageService } from 'src/core/pipeline/services/pipeline-stage.service';
import { PipelineProgressService } from 'src/core/pipeline/services/pipeline-progress.service';
import { PipelineService } from 'src/core/pipeline/services/pipeline.service';
import { PrismaService } from 'src/database/prisma.service';
import { CompanyService } from 'src/core/company/company.service';
import { PersonService } from 'src/core/person/person.service';
import { assert } from 'src/utils/assert';
@Injectable()
export class WorkspaceService {
@ -16,6 +19,7 @@ export class WorkspaceService {
private readonly companyService: CompanyService,
private readonly personService: PersonService,
private readonly pipelineStageService: PipelineStageService,
private readonly pipelineProgressService: PipelineProgressService,
) {}
// Find
@ -81,4 +85,82 @@ export class WorkspaceService {
return workspace;
}
async deleteWorkspace({
workspaceId,
select,
userId,
}: {
workspaceId: string;
select: Prisma.WorkspaceSelect;
userId: string;
}) {
const workspace = await this.findUnique({
where: { id: workspaceId },
select,
});
assert(workspace, 'Workspace not found');
const where = { workspaceId };
const {
user,
workspaceMember,
refreshToken,
attachment,
comment,
commentThreadTarget,
commentThread,
} = this.prismaService.client;
const commentThreads = await commentThread.findMany({
where: { authorId: userId },
});
await this.prismaService.client.$transaction([
this.pipelineProgressService.deleteMany({
where,
}),
this.companyService.deleteMany({
where,
}),
this.personService.deleteMany({
where,
}),
this.pipelineStageService.deleteMany({
where,
}),
this.pipelineService.deleteMany({
where,
}),
workspaceMember.deleteMany({
where,
}),
attachment.deleteMany({
where,
}),
comment.deleteMany({
where,
}),
...commentThreads.map(({ id: commentThreadId }) =>
commentThreadTarget.deleteMany({
where: { commentThreadId },
}),
),
commentThread.deleteMany({
where,
}),
refreshToken.deleteMany({
where: { userId },
}),
user.delete({
where: {
id: userId,
},
}),
this.delete({ where: { id: workspaceId } }),
]);
return workspace;
}
}