Sfoglia il codice sorgente

Implement createCustomerAddress mutation

Michael Bromley 7 anni fa
parent
commit
d387eac4d9

+ 2 - 0
modules/core/api/customer/customer.api.graphql

@@ -6,4 +6,6 @@ type Query {
 type Mutation {
   "Create a new Customer. If a password is provided, a new User will also be created an linked to the Customer."
   createCustomer(input: CreateCustomerInput!, password: String): Customer
+  "Create a new Address and associate it with the Customer specified by customerId"
+  createCustomerAddress(customerId: Int, input: CreateAddressInput): Address
 }

+ 6 - 0
modules/core/api/customer/customer.resolver.ts

@@ -28,4 +28,10 @@ export class CustomerResolver {
         const { input, password } = args;
         return this.customerService.create(input, password);
     }
+
+    @Mutation()
+    createCustomerAddress(_, args): Promise<Address> {
+        const { customerId, input } = args;
+        return this.customerService.createAddress(customerId, input);
+    }
 }

+ 13 - 0
modules/core/entity/address/address.dto.ts

@@ -0,0 +1,13 @@
+export interface CreateAddressDto {
+    fullName: string;
+    company: string;
+    streetLine1: string;
+    streetLine2: string;
+    city: string;
+    province: string;
+    postalCode: string;
+    country: string;
+    phoneNumber: string;
+    defaultShippingAddress: boolean;
+    defaultBillingAddress: boolean;
+}

+ 7 - 1
modules/core/entity/address/address.entity.ts

@@ -1,9 +1,15 @@
 import { Column, CreateDateColumn, Entity, ManyToOne, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';
+import { DeepPartial } from '../../common/common-types';
 import { Customer } from '../customer/customer.entity';
-import { User } from '../user/user.entity';
 
 @Entity('address')
 export class Address {
+    constructor(input?: DeepPartial<Address>) {
+        if (input) {
+            Object.assign(this, input);
+        }
+    }
+
     @PrimaryGeneratedColumn() id: number;
 
     @ManyToOne(type => Customer, customer => customer.addresses)

+ 14 - 2
modules/core/entity/address/address.graphql

@@ -11,6 +11,18 @@ type Address {
   phoneNumber: String
   defaultShippingAddress: Boolean
   defaultBillingAddress: Boolean
-  createdAt: String
-  updatedAt: String
+}
+
+input CreateAddressInput {
+  fullName: String
+  company: String
+  streetLine1: String
+  streetLine2: String
+  city: String
+  province: String
+  postalCode: String
+  country: String
+  phoneNumber: String
+  defaultShippingAddress: Boolean
+  defaultBillingAddress: Boolean
 }

+ 18 - 0
modules/core/service/customer.service.ts

@@ -3,6 +3,7 @@ import { InjectConnection } from '@nestjs/typeorm';
 import { Connection } from 'typeorm';
 import { PasswordService } from '../auth/password.service';
 import { Role } from '../auth/role';
+import { CreateAddressDto } from '../entity/address/address.dto';
 import { Address } from '../entity/address/address.entity';
 import { CreateCustomerDto } from '../entity/customer/customer.dto';
 import { Customer } from '../entity/customer/customer.entity';
@@ -42,4 +43,21 @@ export class CustomerService {
 
         return this.connection.getRepository(Customer).save(customer);
     }
+
+    async createAddress(customerId: number, createAddressDto: CreateAddressDto): Promise<Address> {
+        const customer = await this.connection.manager.findOne(Customer, customerId, { relations: ['addresses'] });
+
+        if (!customer) {
+            throw new Error(`No customer with the id "${customerId}" was found`);
+        }
+
+        const address = new Address(createAddressDto);
+
+        const createdAddress = await this.connection.manager.getRepository(Address).save(address);
+
+        customer.addresses.push(createdAddress);
+        await this.connection.manager.save(customer);
+
+        return createdAddress;
+    }
 }