Skip to content

Commit

Permalink
feat(core): Implement deleteRole mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Nov 13, 2019
1 parent 586f2d7 commit 7b338a4
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,8 @@ export type Mutation = {
createRole: Role,
/** Update an existing Role */
updateRole: Role,
/** Delete an existing Role */
deleteRole: DeletionResponse,
/** Create a new ShippingMethod */
createShippingMethod: ShippingMethod,
/** Update an existing ShippingMethod */
Expand Down Expand Up @@ -2110,6 +2112,11 @@ export type MutationUpdateRoleArgs = {
};


export type MutationDeleteRoleArgs = {
id: Scalars['ID']
};


export type MutationCreateShippingMethodArgs = {
input: CreateShippingMethodInput
};
Expand Down
20 changes: 20 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,8 @@ export type Mutation = {
createRole: Role;
/** Update an existing Role */
updateRole: Role;
/** Delete an existing Role */
deleteRole: DeletionResponse;
/** Create a new ShippingMethod */
createShippingMethod: ShippingMethod;
/** Update an existing ShippingMethod */
Expand Down Expand Up @@ -2053,6 +2055,10 @@ export type MutationUpdateRoleArgs = {
input: UpdateRoleInput;
};

export type MutationDeleteRoleArgs = {
id: Scalars['ID'];
};

export type MutationCreateShippingMethodArgs = {
input: CreateShippingMethodInput;
};
Expand Down Expand Up @@ -4810,6 +4816,14 @@ export type UpdateRoleMutation = { __typename?: 'Mutation' } & {
updateRole: { __typename?: 'Role' } & RoleFragment;
};

export type DeleteRoleMutationVariables = {
id: Scalars['ID'];
};

export type DeleteRoleMutation = { __typename?: 'Mutation' } & {
deleteRole: { __typename?: 'DeletionResponse' } & Pick<DeletionResponse, 'result' | 'message'>;
};

export type ShippingMethodFragment = { __typename?: 'ShippingMethod' } & Pick<
ShippingMethod,
'id' | 'code' | 'description'
Expand Down Expand Up @@ -6055,6 +6069,12 @@ export namespace UpdateRole {
export type UpdateRole = RoleFragment;
}

export namespace DeleteRole {
export type Variables = DeleteRoleMutationVariables;
export type Mutation = DeleteRoleMutation;
export type DeleteRole = DeleteRoleMutation['deleteRole'];
}

export namespace ShippingMethod {
export type Fragment = ShippingMethodFragment;
export type Calculator = ShippingMethodFragment['calculator'];
Expand Down
55 changes: 55 additions & 0 deletions packages/core/e2e/role.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
CreateChannel,
CreateRole,
CurrencyCode,
DeleteRole,
DeletionResult,
GetRole,
GetRoles,
LanguageCode,
Expand Down Expand Up @@ -232,6 +234,50 @@ describe('Role resolver', () => {
}, `The role '${CUSTOMER_ROLE_CODE}' cannot be modified`),
);

it(
'deleteRole is not allowed for Customer role',
assertThrowsWithMessage(async () => {
const customerRole = defaultRoles.find(r => r.code === CUSTOMER_ROLE_CODE);
if (!customerRole) {
fail(`Could not find Customer role`);
return;
}
return adminClient.query<DeleteRole.Mutation, DeleteRole.Variables>(DELETE_ROLE, {
id: customerRole.id,
});
}, `The role '${CUSTOMER_ROLE_CODE}' cannot be deleted`),
);

it(
'deleteRole is not allowed for SuperAdmin role',
assertThrowsWithMessage(async () => {
const superAdminRole = defaultRoles.find(r => r.code === SUPER_ADMIN_ROLE_CODE);
if (!superAdminRole) {
fail(`Could not find Customer role`);
return;
}
return adminClient.query<DeleteRole.Mutation, DeleteRole.Variables>(DELETE_ROLE, {
id: superAdminRole.id,
});
}, `The role '${SUPER_ADMIN_ROLE_CODE}' cannot be deleted`),
);

it('deleteRole deletes a role', async () => {
const { deleteRole } = await adminClient.query<DeleteRole.Mutation, DeleteRole.Variables>(
DELETE_ROLE,
{
id: createdRole.id,
},
);

expect(deleteRole.result).toBe(DeletionResult.DELETED);

const { role } = await adminClient.query<GetRole.Query, GetRole.Variables>(GET_ROLE, {
id: createdRole.id,
});
expect(role).toBeNull();
});

describe('multi-channel', () => {
let secondChannel: CreateChannel.CreateChannel;
let multiChannelRole: CreateRole.CreateRole;
Expand Down Expand Up @@ -337,3 +383,12 @@ export const UPDATE_ROLE = gql`
}
${ROLE_FRAGMENT}
`;

export const DELETE_ROLE = gql`
mutation DeleteRole($id: ID!) {
deleteRole(id: $id) {
result
message
}
}
`;
10 changes: 9 additions & 1 deletion packages/core/src/api/resolvers/admin/role.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import {
DeletionResponse,
MutationCreateRoleArgs,
MutationDeleteRoleArgs,
MutationUpdateRoleArgs,
Permission,
QueryRoleArgs,
Expand Down Expand Up @@ -36,11 +38,17 @@ export class RoleResolver {
const { input } = args;
return this.roleService.create(ctx, input);
}

@Mutation()
@Allow(Permission.UpdateAdministrator)
updateRole(@Ctx() ctx: RequestContext, @Args() args: MutationUpdateRoleArgs): Promise<Role> {
const { input } = args;
return this.roleService.update(ctx, input);
}

@Mutation()
@Allow(Permission.DeleteAdministrator)
deleteRole(@Ctx() ctx: RequestContext, @Args() args: MutationDeleteRoleArgs): Promise<DeletionResponse> {
const { id } = args;
return this.roleService.delete(ctx, id);
}
}
2 changes: 2 additions & 0 deletions packages/core/src/api/schema/admin-api/role.api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type Mutation {
createRole(input: CreateRoleInput!): Role!
"Update an existing Role"
updateRole(input: UpdateRoleInput!): Role!
"Delete an existing Role"
deleteRole(id: ID!): DeletionResponse!
}

# generated by generateListOptions function
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/i18n/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"cancel-order-lines-invalid-order-state": "Cannot cancel OrderLines from an Order in the \"{ state }\" state",
"cancel-order-lines-nothing-to-cancel": "Nothing to cancel",
"cancel-order-lines-quantity-too-high": "Quantity to cancel is greater than existing OrderLine quantity",
"cannot-delete-role": "The role '{ roleCode }' cannot be deleted",
"cannot-modify-role": "The role '{ roleCode }' cannot be modified",
"cannot-create-sales-for-active-order": "Cannot create a Sale for an Order which is still active",
"cannot-move-collection-into-self": "Cannot move a Collection into itself",
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/service/services/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export class ChannelService {
}

async delete(id: ID): Promise<DeletionResponse> {
await getEntityOrThrow(this.connection, Channel, id);
await this.connection.getRepository(Channel).delete(id);
await this.connection.getRepository(ProductVariantPrice).delete({
channelId: id,
Expand Down
22 changes: 21 additions & 1 deletion packages/core/src/service/services/role.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Injectable } from '@nestjs/common';
import { InjectConnection } from '@nestjs/typeorm';
import { CreateRoleInput, Permission, UpdateRoleInput } from '@vendure/common/lib/generated-types';
import {
CreateRoleInput,
DeletionResponse,
DeletionResult,
Permission,
UpdateRoleInput,
} from '@vendure/common/lib/generated-types';
import {
CUSTOMER_ROLE_CODE,
CUSTOMER_ROLE_DESCRIPTION,
Expand Down Expand Up @@ -148,6 +154,20 @@ export class RoleService {
return assertFound(this.findOne(role.id));
}

async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
const role = await this.findOne(id);
if (!role) {
throw new EntityNotFoundError('Role', id);
}
if (role.code === SUPER_ADMIN_ROLE_CODE || role.code === CUSTOMER_ROLE_CODE) {
throw new InternalServerError(`error.cannot-delete-role`, { roleCode: role.code });
}
await this.connection.getRepository(Role).remove(role);
return {
result: DeletionResult.DELETED,
};
}

async assignRoleToChannel(roleId: ID, channelId: ID) {
await this.channelService.assignToChannels(Role, roleId, [channelId]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,8 @@ export type Mutation = {
createRole: Role;
/** Update an existing Role */
updateRole: Role;
/** Delete an existing Role */
deleteRole: DeletionResponse;
/** Create a new ShippingMethod */
createShippingMethod: ShippingMethod;
/** Update an existing ShippingMethod */
Expand Down Expand Up @@ -2053,6 +2055,10 @@ export type MutationUpdateRoleArgs = {
input: UpdateRoleInput;
};

export type MutationDeleteRoleArgs = {
id: Scalars['ID'];
};

export type MutationCreateShippingMethodArgs = {
input: CreateShippingMethodInput;
};
Expand Down
2 changes: 1 addition & 1 deletion schema-admin.json

Large diffs are not rendered by default.

0 comments on commit 7b338a4

Please sign in to comment.