Skip to content

Commit

Permalink
feat(core): Implement deletion of TaxRate
Browse files Browse the repository at this point in the history
Relates to #262
  • Loading branch information
michaelbromley committed Feb 12, 2020
1 parent b263b8b commit 8c2db90
Show file tree
Hide file tree
Showing 8 changed files with 100 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 @@ -1807,6 +1807,8 @@ export type Mutation = {
createTaxRate: TaxRate,
/** Update an existing TaxRate */
updateTaxRate: TaxRate,
/** Delete a TaxRate */
deleteTaxRate: DeletionResponse,
/** Create a new Zone */
createZone: Zone,
/** Update an existing Zone */
Expand Down Expand Up @@ -2171,6 +2173,11 @@ export type MutationUpdateTaxRateArgs = {
};


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


export type MutationCreateZoneArgs = {
input: CreateZoneInput
};
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 @@ -1810,6 +1810,8 @@ export type Mutation = {
createTaxRate: TaxRate;
/** Update an existing TaxRate */
updateTaxRate: TaxRate;
/** Delete a TaxRate */
deleteTaxRate: DeletionResponse;
/** Create a new Zone */
createZone: Zone;
/** Update an existing Zone */
Expand Down Expand Up @@ -2105,6 +2107,10 @@ export type MutationUpdateTaxRateArgs = {
input: UpdateTaxRateInput;
};

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

export type MutationCreateZoneArgs = {
input: CreateZoneInput;
};
Expand Down Expand Up @@ -5159,6 +5165,14 @@ export type CreateTaxRateMutation = { __typename?: 'Mutation' } & {
createTaxRate: { __typename?: 'TaxRate' } & TaxRateFragment;
};

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

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

export type DeleteZoneMutationVariables = {
id: Scalars['ID'];
};
Expand Down Expand Up @@ -6409,6 +6423,12 @@ export namespace CreateTaxRate {
export type CreateTaxRate = TaxRateFragment;
}

export namespace DeleteTaxRate {
export type Variables = DeleteTaxRateMutationVariables;
export type Mutation = DeleteTaxRateMutation;
export type DeleteTaxRate = DeleteTaxRateMutation['deleteTaxRate'];
}

export namespace DeleteZone {
export type Variables = DeleteZoneMutationVariables;
export type Mutation = DeleteZoneMutation;
Expand Down
33 changes: 32 additions & 1 deletion packages/core/e2e/tax-rate.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { initialData } from '../../../e2e-common/e2e-initial-data';
import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';

import { TAX_RATE_FRAGMENT } from './graphql/fragments';
import { CreateTaxRate, GetTaxRate, GetTaxRates, UpdateTaxRate } from './graphql/generated-e2e-admin-types';
import {
CreateTaxRate,
DeleteTaxRate,
DeletionResult,
GetTaxRate,
GetTaxRates,
UpdateTaxRate,
} from './graphql/generated-e2e-admin-types';
import { UPDATE_TAX_RATE } from './graphql/shared-definitions';

describe('TaxRate resolver', () => {
Expand Down Expand Up @@ -79,6 +86,21 @@ describe('TaxRate resolver', () => {

expect(updateTaxRate.value).toBe(17.5);
});

it('deleteTaxRate', async () => {
const { deleteTaxRate } = await adminClient.query<DeleteTaxRate.Mutation, DeleteTaxRate.Variables>(
DELETE_TAX_RATE,
{
id: 'T_3',
},
);

expect(deleteTaxRate.result).toBe(DeletionResult.DELETED);
expect(deleteTaxRate.message).toBeNull();

const { taxRates } = await adminClient.query<GetTaxRates.Query>(GET_TAX_RATES_LIST);
expect(taxRates.items.find(x => x.id === 'T_3')).toBeUndefined();
});
});

export const GET_TAX_RATES_LIST = gql`
Expand Down Expand Up @@ -110,3 +132,12 @@ export const CREATE_TAX_RATE = gql`
}
${TAX_RATE_FRAGMENT}
`;

export const DELETE_TAX_RATE = gql`
mutation DeleteTaxRate($id: ID!) {
deleteTaxRate(id: $id) {
result
message
}
}
`;
11 changes: 11 additions & 0 deletions packages/core/src/api/resolvers/admin/tax-rate.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,
MutationCreateTaxRateArgs,
MutationDeleteTaxRateArgs,
MutationUpdateTaxRateArgs,
Permission,
QueryTaxRateArgs,
Expand Down Expand Up @@ -47,4 +49,13 @@ export class TaxRateResolver {
): Promise<TaxRate> {
return this.taxRateService.update(ctx, args.input);
}

@Mutation()
@Allow(Permission.DeleteSettings)
async deleteTaxRate(
@Ctx() ctx: RequestContext,
@Args() args: MutationDeleteTaxRateArgs,
): Promise<DeletionResponse> {
return this.taxRateService.delete(args.id);
}
}
2 changes: 2 additions & 0 deletions packages/core/src/api/schema/admin-api/tax-rate.api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type Mutation {
createTaxRate(input: CreateTaxRateInput!): TaxRate!
"Update an existing TaxRate"
updateTaxRate(input: UpdateTaxRateInput!): TaxRate!
"Delete a TaxRate"
deleteTaxRate(id: ID!): DeletionResponse!
}

# generated by generateListOptions function
Expand Down
22 changes: 21 additions & 1 deletion packages/core/src/service/services/tax-rate.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { InjectConnection } from '@nestjs/typeorm';
import { CreateTaxRateInput, UpdateTaxRateInput } from '@vendure/common/lib/generated-types';
import {
CreateTaxRateInput,
DeletionResponse,
DeletionResult,
UpdateTaxRateInput,
} from '@vendure/common/lib/generated-types';
import { ID, PaginatedList } from '@vendure/common/lib/shared-types';
import { Connection } from 'typeorm';

Expand Down Expand Up @@ -103,6 +108,21 @@ export class TaxRateService {
return assertFound(this.findOne(taxRate.id));
}

async delete(id: ID): Promise<DeletionResponse> {
const taxRate = await getEntityOrThrow(this.connection, TaxRate, id);
try {
await this.connection.getRepository(TaxRate).remove(taxRate);
return {
result: DeletionResult.DELETED,
};
} catch (e) {
return {
result: DeletionResult.NOT_DELETED,
message: e.toString(),
};
}
}

getActiveTaxRates(): TaxRate[] {
return this.activeTaxRates;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,8 @@ export type Mutation = {
createTaxRate: TaxRate;
/** Update an existing TaxRate */
updateTaxRate: TaxRate;
/** Delete a TaxRate */
deleteTaxRate: DeletionResponse;
/** Create a new Zone */
createZone: Zone;
/** Update an existing Zone */
Expand Down Expand Up @@ -2105,6 +2107,10 @@ export type MutationUpdateTaxRateArgs = {
input: UpdateTaxRateInput;
};

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

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

Large diffs are not rendered by default.

0 comments on commit 8c2db90

Please sign in to comment.