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

Add deleteDraftProject GQL mutation #796

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 20 additions & 0 deletions backend/LexBoxApi/GraphQL/ProjectMutations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,26 @@ await dbContext.ProjectUsers.Where(pu => pu.ProjectId == input.ProjectId && pu.U
return dbContext.Projects.Where(p => p.Id == input.ProjectId);
}

[Error<NotFoundException>]
[Error<DbError>]
[UseMutationConvention]
[UseFirstOrDefault]
[UseProjection]
public async Task<IQueryable<DraftProject>> DeleteDraftProject(
rmunn marked this conversation as resolved.
Show resolved Hide resolved
Guid draftProjectId,
IPermissionService permissionService,
LexBoxDbContext dbContext)
{
permissionService.AssertCanManageProject(draftProjectId);

// Draft projects are deleted immediately, not soft-deleted
var deletedDraftCount = await dbContext.DraftProjects.Where(dp => dp.Id == draftProjectId).ExecuteDeleteAsync();
if (deletedDraftCount == 0)
{
throw NotFoundException.ForType<DraftProject>();
}
return dbContext.DraftProjects.Where(p => p.Id == draftProjectId);
rmunn marked this conversation as resolved.
Show resolved Hide resolved
}

[Error<NotFoundException>]
[Error<DbError>]
Expand Down
12 changes: 12 additions & 0 deletions frontend/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ type DbError implements Error {
code: DbErrorCode!
}

type DeleteDraftProjectPayload {
draftProject: DraftProject
errors: [DeleteDraftProjectError!]
}

type DeleteUserByAdminOrSelfPayload {
user: User
errors: [DeleteUserByAdminOrSelfError!]
Expand Down Expand Up @@ -176,6 +181,7 @@ type Mutation {
setProjectConfidentiality(input: SetProjectConfidentialityInput!): SetProjectConfidentialityPayload!
leaveProject(input: LeaveProjectInput!): LeaveProjectPayload!
removeProjectMember(input: RemoveProjectMemberInput!): RemoveProjectMemberPayload!
deleteDraftProject(input: DeleteDraftProjectInput!): DeleteDraftProjectPayload!
softDeleteProject(input: SoftDeleteProjectInput!): SoftDeleteProjectPayload!
changeUserAccountBySelf(input: ChangeUserAccountBySelfInput!): ChangeUserAccountBySelfPayload!
changeUserAccountByAdmin(input: ChangeUserAccountByAdminInput!): ChangeUserAccountByAdminPayload! @authorize(policy: "AdminRequiredPolicy")
Expand Down Expand Up @@ -360,6 +366,8 @@ union CreateOrganizationError = DbError

union CreateProjectError = DbError | AlreadyExistsError | ProjectCreatorsMustHaveEmail

union DeleteDraftProjectError = NotFoundError | DbError

union DeleteUserByAdminOrSelfError = NotFoundError | DbError

union LeaveProjectError = NotFoundError | LastMemberCantLeaveError
Expand Down Expand Up @@ -450,6 +458,10 @@ input DateTimeOperationFilterInput {
nlte: DateTime
}

input DeleteDraftProjectInput {
draftProjectId: UUID!
}

input DeleteUserByAdminOrSelfInput {
userId: UUID!
}
Expand Down
29 changes: 28 additions & 1 deletion frontend/src/lib/gql/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { $OpResult, DeleteUserByAdminOrSelfInput, DeleteUserByAdminOrSelfMutation, SoftDeleteProjectMutation } from './types';
import type { $OpResult, DeleteUserByAdminOrSelfInput, DeleteUserByAdminOrSelfMutation, SoftDeleteProjectMutation, DeleteDraftProjectMutation } from './types';

import { getClient } from './gql-client';
import { graphql } from './generated';
Expand Down Expand Up @@ -55,3 +55,30 @@ export async function _deleteProject(projectId: string): $OpResult<SoftDeletePro

return result;
}

export async function _deleteDraftProject(draftProjectId: string): $OpResult<DeleteDraftProjectMutation> {
//language=GraphQL
const result = await getClient()
.mutation(
graphql(`
mutation DeleteDraftProject($input: DeleteDraftProjectInput!) {
deleteDraftProject(input: $input) {
draftProject {
id,
}
errors {
... on Error {
message
}
}
}
}
`),
{
input: { draftProjectId }
},
{ additionalTypenames: ['Projects'] },
);

return result;
}
22 changes: 19 additions & 3 deletions frontend/src/routes/(authenticated)/admin/AdminProjects.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import {DialogResponse} from '$lib/components/modals';
import ConfirmDeleteModal from '$lib/components/modals/ConfirmDeleteModal.svelte';
import {Button} from '$lib/forms';
import {_deleteProject} from '$lib/gql/mutations';
import {_deleteProject, _deleteDraftProject} from '$lib/gql/mutations';
import t, {number} from '$lib/i18n';
import {TrashIcon} from '$lib/icons';
import {useNotifications} from '$lib/notify';
Expand Down Expand Up @@ -59,7 +59,7 @@
$: shownProjects = limitResults ? limit(filteredProjects, lastLoadUsedActiveFilter ? DEFAULT_PAGE_SIZE : 10) : filteredProjects;

let deleteProjectModal: ConfirmDeleteModal;
async function softDeleteProject(project: ProjectItemWithDraftStatus): Promise<void> {
async function softDeleteProject(project: ProjectItem): Promise<void> {
const result = await deleteProjectModal.open(project.name, async () => {
const { error } = await _deleteProject(project.id);
rmunn marked this conversation as resolved.
Show resolved Hide resolved
return error?.message;
Expand All @@ -68,6 +68,22 @@
notifyWarning($t('delete_project_modal.success', { name: project.name, code: project.code }));
}
}
async function deleteDraftProject(project: DraftProject): Promise<void> {
const result = await deleteProjectModal.open(project.name, async () => {
const { error } = await _deleteDraftProject(project.id);
return error?.message;
});
if (result.response === DialogResponse.Submit) {
notifyWarning($t('delete_project_modal.success', { name: project.name, code: project.code }));
}
}
function deleteProjectOrDraft(project: ProjectItemWithDraftStatus): Promise<void> {
if (project.isDraft) {
return deleteDraftProject(project)
} else {
return softDeleteProject(project);
}
}
</script>

<ConfirmDeleteModal bind:this={deleteProjectModal} i18nScope="delete_project_modal" />
Expand Down Expand Up @@ -117,7 +133,7 @@
</button>
<ul slot="content" class="menu">
<li>
<button class="text-error whitespace-nowrap" on:click={() => softDeleteProject(project)}>
<button class="text-error whitespace-nowrap" on:click={() => deleteProjectOrDraft(project)}>
<TrashIcon />
{$t('delete_project_modal.submit')}
</button>
Expand Down
Loading