소스 검색

feat(core): Implement deletion of TaxRate

Relates to #262
Michael Bromley 6 년 전
부모
커밋
8c2db907ab

+ 7 - 0
packages/common/src/generated-types.ts

@@ -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 */
@@ -2171,6 +2173,11 @@ export type MutationUpdateTaxRateArgs = {
 };
 
 
+export type MutationDeleteTaxRateArgs = {
+  id: Scalars['ID']
+};
+
+
 export type MutationCreateZoneArgs = {
   input: CreateZoneInput
 };

+ 20 - 0
packages/core/e2e/graphql/generated-e2e-admin-types.ts

@@ -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 */
@@ -2105,6 +2107,10 @@ export type MutationUpdateTaxRateArgs = {
     input: UpdateTaxRateInput;
 };
 
+export type MutationDeleteTaxRateArgs = {
+    id: Scalars['ID'];
+};
+
 export type MutationCreateZoneArgs = {
     input: CreateZoneInput;
 };
@@ -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'];
 };
@@ -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;

+ 32 - 1
packages/core/e2e/tax-rate.e2e-spec.ts

@@ -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', () => {
@@ -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`
@@ -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 - 0
packages/core/src/api/resolvers/admin/tax-rate.resolver.ts

@@ -1,6 +1,8 @@
 import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
 import {
+    DeletionResponse,
     MutationCreateTaxRateArgs,
+    MutationDeleteTaxRateArgs,
     MutationUpdateTaxRateArgs,
     Permission,
     QueryTaxRateArgs,
@@ -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 - 0
packages/core/src/api/schema/admin-api/tax-rate.api.graphql

@@ -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

+ 21 - 1
packages/core/src/service/services/tax-rate.service.ts

@@ -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';
 
@@ -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;
     }

+ 6 - 0
packages/elasticsearch-plugin/e2e/graphql/generated-e2e-elasticsearch-plugin-types.ts

@@ -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 */
@@ -2105,6 +2107,10 @@ export type MutationUpdateTaxRateArgs = {
     input: UpdateTaxRateInput;
 };
 
+export type MutationDeleteTaxRateArgs = {
+    id: Scalars['ID'];
+};
+
 export type MutationCreateZoneArgs = {
     input: CreateZoneInput;
 };

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
schema-admin.json


이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.