Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headless/ismail0946/bc 488/customer/change password api #321

Open
wants to merge 8 commits into
base: new-techstack-monorepo-backup
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CartModule } from 'src/modules/cart/cart.graphql.module';
import { MediaModule } from 'src/modules/media/media.graphql.module';
import { WishListModule } from 'src/modules/wishlist/wishlist.graphql.module';
import { TagsModule } from 'src/modules/tags/tags.graphql.module';
import { CustomerModule } from 'src/modules/customer/customer.graphql.module';

export const ResolveGraphqlModule = () => {
return [
Expand All @@ -29,5 +30,6 @@ export const ResolveGraphqlModule = () => {
CompareModule,
WishListModule,
TagsModule,
CustomerModule,
];
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Module } from '@nestjs/common';
import { ResolveDatabaseDependency } from 'src/database/database.resolver';
import { CUstomerResolver } from './graphql/customer.resolver';
import { CustomerResolver } from './graphql/customer.resolver';
import { CustomerRepository } from './repositories';
import { ICustomerDatabase } from './repositories/customer.database.interface';
import { CustomerService } from './services';

@Module({
controllers: [],
providers: [
CUstomerResolver,
CustomerResolver,
CustomerService,
CustomerRepository,
{
Expand Down
Empty file.
145 changes: 145 additions & 0 deletions packages/headless/src/modules/customer/graphql/customer.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { Field, InputType, Int, ObjectType } from '@nestjs/graphql';
import {
Customer,
CustomerAddress,
UpdateCustomerRequestBody,
} from 'models';

@ObjectType('CustomerAddress')
export class GraphqlCustomerAddress implements CustomerAddress {
@Field({ nullable: true })
id?: string;

@Field()
firstName: string;

@Field()
lastName: string;

@Field()
addressLine1: string;

@Field({ nullable: true })
addressLine2?: string;

@Field({ nullable: true })
company?: string;

@Field({ nullable: true })
state?: string;

@Field({ nullable: true })
country?: string;

@Field({ nullable: true })
postCode?: string;

@Field()
phone: string;

@Field()
tag: string;
}

@InputType()
export class CustomerAddressInput {
@Field()
firstName: string;

@Field()
lastName: string;

@Field()
addressLine1: string;

@Field()
addressLine2?: string;

@Field({ nullable: true })
company?: string;

@Field({ nullable: true })
state?: string;

@Field({ nullable: true })
country?: string;

@Field({ nullable: true })
postCode?: string;

@Field()
phone: string;

@Field()
tag: string;
}

@ObjectType('Customer')
export class GraphqlCustomer implements Customer {
@Field()
id: string;

@Field({ nullable: true })
firstName?: string;

@Field({ nullable: true })
lastName?: string;

@Field({ nullable: true })
phone?: string;

@Field({ nullable: true })
email?: string;

@Field(() => [GraphqlCustomerAddress], { nullable: true })
addresses?: GraphqlCustomerAddress[];
}

@InputType()
export class UpdateCustomerInput implements UpdateCustomerRequestBody {
@Field({ nullable: true })
firstName?: string;

@Field({ nullable: true })
lastName?: string;

@Field({ nullable: true })
phone?: string;

@Field({ nullable: true })
email?: string;
}

@InputType()
export class CustomerChangePasswordInput {
@Field()
currentPassword: string;

@Field()
newPassword: string;
}

@ObjectType()
export class CustomerResponse {
@Field(() => Int)
code: number;

@Field(() => GraphqlCustomer, { nullable: true })
data?: GraphqlCustomer;
}


@ObjectType()
export class CustomerChangePasswordResponseMessage {
@Field()
message: string;
}

@ObjectType()
export class CustomerChangePasswordResponse {
@Field(() => Int)
code: number;

@Field(() => CustomerChangePasswordResponseMessage, { nullable: true })
data?: CustomerChangePasswordResponseMessage;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,53 @@
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { User } from 'src/entity/user';
import { User as UserInfo } from 'src/decorators/auth.decorator';
import { User as CustomerInfo } from 'src/decorators/auth.decorator';
import { UseGuards } from '@nestjs/common';
import { RolesGuard } from 'src/guards/auth.guard';
import { CustomerService } from '../services';
import { Customer } from 'src/entity/customer';
import {
CustomerAddressInput,
CustomerResponse,
UpdateCustomerInput
} from './customer.model';
import { Helper } from 'src/helper/helper.interface';

@UseGuards(new RolesGuard(['customer']))
@Resolver()
export class CUstomerResolver {
constructor(private userService: CustomerService) { }
export class CustomerResolver {
constructor(private customerService: CustomerService, private helper: Helper) { }

@Query(() => CustomerResponse)
async getCustomerInfo(@CustomerInfo() customer: Customer) {
const res = await this.customerService.getCustomer(customer.id);
return this.helper.serviceResponse.graphqlResponse(res);
}

@Mutation(() => CustomerResponse)
async updateCustomer(@Args('data') data: UpdateCustomerInput, @CustomerInfo() customer: Customer) {
const res = await this.customerService.updateCustomer(customer.id, data);
return this.helper.serviceResponse.graphqlResponse(res);
}

@Mutation(() => CustomerResponse)
async addCustomerNewAddress(@Args('address') address: CustomerAddressInput, @CustomerInfo() customer: Customer) {
const res = await this.customerService.addCustomerNewAddress(customer.id, address);
return this.helper.serviceResponse.graphqlResponse(res);
}

@Mutation(() => CustomerResponse)
async updateCustomerAddress(@Args('address') address: CustomerAddressInput, @Args('addressId') addressId: string, @CustomerInfo() customer: Customer) {
const res = await this.customerService.updateCustomerAddress(customer.id, addressId, { ...address, id: addressId });
return this.helper.serviceResponse.graphqlResponse(res);
}

@Mutation(() => CustomerResponse)
async deleteCustomerAddress(@Args('addressId') addressId: string, @CustomerInfo() customer: Customer) {
const res = await this.customerService.deleteCustomerAddress(customer.id, addressId);
return this.helper.serviceResponse.graphqlResponse(res);
}

/* @Mutation()
async changePassword(@Args('passwordDetails') passwordDetails: CustomerChangePasswordInput, @CustomerInfo() customer: Customer) {
const res = await this.customerService.changePassword(customer.id, passwordDetails);
} */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { HttpStatus } from '@nestjs/common';
import { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsNotEmpty, IsNumber, IsObject, IsString, MinLength } from 'class-validator';
import {
CustomerChangePasswordErrorResponse,
CustomerChangePasswordRequest,
CustomerChangePasswordSuccessResponse,
CustomerChangePasswordErrorMessages,
CustomerChangePasswordSuccessMessage
} from 'models';

export class CustomerChangePasswordDto implements CustomerChangePasswordRequest {
@ApiProperty()
@IsNotEmpty()
@IsString()
@MinLength(6)
currentPassword: string;

@ApiProperty()
@IsNotEmpty()
@IsString()
@MinLength(6)
newPassword: string;
}

export class CustomerChangePasswordErrorResponseDto implements CustomerChangePasswordErrorResponse {
@ApiProperty({ default: HttpStatus.BAD_REQUEST })
@IsNumber()
code: number;

@ApiProperty({
example: CustomerChangePasswordErrorMessages.CAN_NOT_CHANGE_PASSWORD,
examples: [CustomerChangePasswordErrorMessages.CAN_NOT_GET_CUSTOMER, CustomerChangePasswordErrorMessages.CURRENT_PASSWORD_IS_INCORRECT, CustomerChangePasswordErrorMessages.CAN_NOT_CHANGE_PASSWORD]
})
error: CustomerChangePasswordErrorMessages;

@ApiProperty()
@IsArray()
errors: string[];
}

class Message {
@ApiProperty({ example: CustomerChangePasswordSuccessMessage.CHANGE_PASSWORD_SUCCESSFUL })
@IsString()
message: CustomerChangePasswordSuccessMessage.CHANGE_PASSWORD_SUCCESSFUL;
}
export class CustomerChangePasswordSuccessResponseDto implements CustomerChangePasswordSuccessResponse {
@ApiProperty({ default: HttpStatus.OK })
@IsNumber()
code: number;

@ApiProperty()
@IsObject()
data: Message;
}
3 changes: 2 additions & 1 deletion packages/headless/src/modules/customer/rest/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from './customer.dto';
export * from './addCustomerNewAddress.dto';
export * from './updateCustomerAddress.dto';
export * from './deleteCustomerAddress';
export * from './updateCustomer.dto';
export * from './updateCustomer.dto';
export * from './changePassword.dto';
Loading