Jelajahi Sumber

Implement createCustomer mutation

Michael Bromley 7 tahun lalu
induk
melakukan
07061a1f4c

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

@@ -2,3 +2,8 @@ type Query {
   customers: [Customer]
   customer(id: Int!): Customer
 }
+
+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
+}

+ 8 - 1
modules/core/api/customer/customer.resolver.ts

@@ -1,5 +1,6 @@
-import { Query, ResolveProperty, Resolver } from '@nestjs/graphql';
+import { Mutation, Query, ResolveProperty, Resolver } from '@nestjs/graphql';
 import { Address } from '../../entity/address/address.entity';
+import { CreateCustomerDto } from '../../entity/customer/customer.dto';
 import { Customer } from '../../entity/customer/customer.entity';
 import { CustomerService } from '../../service/customer.service';
 
@@ -21,4 +22,10 @@ export class CustomerResolver {
     addresses(customer: Customer): Promise<Address[]> {
         return this.customerService.findAddressesByCustomerId(customer.id);
     }
+
+    @Mutation()
+    createCustomer(_, args): Promise<Customer> {
+        const { input, password } = args;
+        return this.customerService.create(input, password);
+    }
 }

+ 1 - 1
modules/core/api/product/product.resolver.ts

@@ -34,7 +34,7 @@ export class ProductResolver {
 
     @Mutation()
     updateProduct(_, args): Promise<Product | undefined> {
-        const { productId, input } = args;
+        const { input } = args;
         return this.productService.update(input);
     }
 }

+ 6 - 0
modules/core/entity/customer/customer.dto.ts

@@ -0,0 +1,6 @@
+export interface CreateCustomerDto {
+    firstName?: string;
+    lastName?: string;
+    phoneNumber?: string;
+    emailAddress: string;
+}

+ 7 - 0
modules/core/entity/customer/customer.graphql

@@ -7,3 +7,10 @@ type Customer {
   addresses: [Address]
   user: User
 }
+
+input CreateCustomerInput {
+  firstName: String
+  lastName: String
+  phoneNumber: String
+  emailAddress: String!
+}

+ 20 - 1
modules/core/service/customer.service.ts

@@ -1,12 +1,16 @@
 import { Injectable } from '@nestjs/common';
 import { InjectConnection } from '@nestjs/typeorm';
 import { Connection } from 'typeorm';
+import { PasswordService } from '../auth/password.service';
+import { Role } from '../auth/role';
 import { Address } from '../entity/address/address.entity';
+import { CreateCustomerDto } from '../entity/customer/customer.dto';
 import { Customer } from '../entity/customer/customer.entity';
+import { User } from '../entity/user/user.entity';
 
 @Injectable()
 export class CustomerService {
-    constructor(@InjectConnection() private connection: Connection) {}
+    constructor(@InjectConnection() private connection: Connection, private passwordService: PasswordService) {}
 
     findAll(): Promise<Customer[]> {
         return this.connection.manager.find(Customer);
@@ -23,4 +27,19 @@ export class CustomerService {
             .where('address.customerId = :id', { id: customerId })
             .getMany();
     }
+
+    async create(createCustomerDto: CreateCustomerDto, password?: string): Promise<Customer> {
+        const customer = new Customer(createCustomerDto);
+
+        if (password) {
+            const user = new User();
+            user.passwordHash = await this.passwordService.hash(password);
+            user.identifier = createCustomerDto.emailAddress;
+            user.roles = [Role.Customer];
+            const createdUser = await this.connection.getRepository(User).save(user);
+            customer.user = createdUser;
+        }
+
+        return this.connection.getRepository(Customer).save(customer);
+    }
 }