Skip to content

Commit

Permalink
FES-52 create API to update customer profile (#6)
Browse files Browse the repository at this point in the history
* FES-52 create API to update customer profile

* FES-52 fix comment 19/02

* FES-52 fix comment 19/02

* FES-52 fix comment 22/02

* FES-52 fix comment 22/02 noon

---------

Co-authored-by: Billy <[email protected]>
  • Loading branch information
manhtran150394 and Billy authored Feb 22, 2024
1 parent da1f3f0 commit a53a50f
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/feature/customer/customer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { CreateCustomerProfileRequest } from './dto/create-customer-profile-requ
import { CreateCustomerProfileResponse } from './dto/create-customer-profile-response.dto';
import { Customer } from 'src/entity/customer.entity';
import { NutiExpertService } from 'src/dependency/nuti-expert/nuti-expert.service';
import { UpdateCustomerProfileRequest } from './dto/update-customer-profile-request.dto';
import { UpdateCustomerProfileResponse } from './dto/update-customer-profile-response.dto';

@Controller()
export class CustomerController {
Expand Down Expand Up @@ -80,4 +82,58 @@ export class CustomerController {
async getCustomerProfile(id: number) {
return await this.customerService.getCustomerProfile(id);
}

@MessagePattern({ cmd: 'update_customer_profile' })
async updateProfile(
data: UpdateCustomerProfileRequest,
): Promise<UpdateCustomerProfileResponse> {
const response: UpdateCustomerProfileResponse =
new UpdateCustomerProfileResponse(200, '');
const {
customer_id,
name,
email,
birthday,
sex,
height_m,
weight_kg,
physical_activity_level,
current_diet,
allergic_food,
chronic_disease,
expected_diet,
} = data.requestData;

try {
const customer: Customer =
await this.customerService.updateCustomerProfile(
height_m,
weight_kg,
physical_activity_level,
current_diet,
allergic_food,
chronic_disease,
expected_diet,
name,
email,
birthday,
sex,
customer_id,
);
response.statusCode = 200;
response.message = 'Update customer profile successfully';
response.data = customer;
} catch (error) {
if (error instanceof HttpException) {
response.statusCode = error.getStatus();
response.message = error.getResponse();
response.data = null;
} else {
response.statusCode = 500;
response.message = error.toString();
response.data = null;
}
}
return response;
}
}
140 changes: 140 additions & 0 deletions src/feature/customer/customer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Repository } from 'typeorm';
import { Media } from 'src/entity/media.entity';
import { GeneralResponse } from 'src/dto/general-response.dto';
import { PhysicalActivityLevel } from 'src/enum';
import { NutiExpertService } from 'src/dependency/nuti-expert/nuti-expert.service';

@Injectable()
export class CustomerService {
Expand All @@ -15,6 +16,7 @@ export class CustomerService {
@InjectRepository(HealthInfo)
private healthInfoRepo: Repository<HealthInfo>,
@InjectRepository(Media) private mediaRepo: Repository<Media>,
private readonly nutiExperService: NutiExpertService,
) {}
async createProfile(
height_m: number,
Expand Down Expand Up @@ -98,4 +100,142 @@ export class CustomerService {
return response;
}
}
async updateCustomerProfile(
height_m: number,
weight_kg: number,
physical_activity_level: PhysicalActivityLevel,
current_diet: string = '',
allergic_food: string = '',
chronic_disease: string = '',
expected_diet: string = '',
name: string,
email: string,
birthday: string,
sex: string,
customer_id: number,
): Promise<Customer> {
/*
//Check whether this profile existed
*/
const customer = await this.customerRepo.findOne({
relations: {
profile_image: true,
health_info: true,
},
where: {
customer_id: customer_id,
},
});
if (!customer) {
throw new HttpException(`Profile doesn't exist`, 400);
}
/*
//Check whether health info existed
*/
if (!customer.health_info) {
throw new HttpException(`Health info doesn't exist`, 500);
}
/*
//Convertion due to diff of findNutritionSuggestion func & inputed birthday
*/
const new_birthday: Date = new Date(birthday);
const old_birthday: Date = new Date(customer.birthday);

let isBirthdayChangedFlg: boolean = false;
if (new_birthday.getTime() !== old_birthday.getTime()) {
isBirthdayChangedFlg = true;
}
/*
//Update only if there is an change in customer info
*/
if (
name === customer.name &&
email === customer.email &&
!isBirthdayChangedFlg &&
sex === customer.sex &&
height_m === customer.health_info.height_m &&
weight_kg === customer.health_info.weight_kg &&
physical_activity_level ===
customer.health_info.physical_activity_level &&
current_diet === customer.health_info.current_diet &&
allergic_food === customer.health_info.allergic_food &&
chronic_disease === customer.health_info.chronic_disease &&
expected_diet === customer.health_info.expected_diet
) {
throw new HttpException(`Need changes!`, 400);
}
/*
//Update health info if there is a change related to health info
*/
let isHealthInfoChangedFlg: boolean = false;
if (
isBirthdayChangedFlg ||
sex !== customer.sex ||
height_m !== customer.health_info.height_m ||
weight_kg !== customer.health_info.weight_kg ||
physical_activity_level !==
customer.health_info.physical_activity_level ||
current_diet !== customer.health_info.current_diet ||
allergic_food !== customer.health_info.allergic_food ||
chronic_disease !== customer.health_info.chronic_disease ||
expected_diet !== customer.health_info.expected_diet
) {
isHealthInfoChangedFlg = true;
const healthInfo = await this.healthInfoRepo.findOneBy({
health_info_id: customer.health_info.health_info_id,
});
healthInfo.height_m = height_m;
healthInfo.weight_kg = weight_kg;
healthInfo.physical_activity_level = physical_activity_level;
healthInfo.current_diet = current_diet;
healthInfo.allergic_food = allergic_food;
healthInfo.chronic_disease = chronic_disease;
healthInfo.expected_diet = expected_diet;
/*
//Recalculate BMI & Recommended dietary allowance kcal
//Only if there is an update in:
//birthday, height, weight, sex, physical_activity_level
*/
if (
isBirthdayChangedFlg ||
sex !== customer.sex ||
height_m !== healthInfo.height_m ||
weight_kg !== healthInfo.weight_kg ||
physical_activity_level !== healthInfo.physical_activity_level
) {
const { bmi, recommended_dietary_allowance_kcal } =
await this.nutiExperService.findNutritionSuggestion(
birthday,
height_m,
weight_kg,
sex,
physical_activity_level,
);
healthInfo.bmi = bmi;
healthInfo.recommended_dietary_allowance_kcal =
recommended_dietary_allowance_kcal;
}
const newHealthInfo = await this.healthInfoRepo.save(healthInfo);
customer.health_info = newHealthInfo;
}
/*
//Update Customer
*/
if (
name !== customer.name ||
email !== customer.email ||
isBirthdayChangedFlg ||
sex !== customer.sex ||
isHealthInfoChangedFlg
) {
customer.name = name;
customer.email = email;
customer.birthday = new_birthday;
customer.sex = sex;
// customer.is_active = 1;
const updatedCustomer = await this.customerRepo.save(customer);
delete updatedCustomer.refresh_token;
return updatedCustomer;
}
}
}
19 changes: 19 additions & 0 deletions src/feature/customer/dto/update-customer-profile-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PhysicalActivityLevel } from 'src/enum';

export class UpdateCustomerProfileRequest {
requestData: requestData;
}
class requestData {
customer_id: number;
name: string;
email: string;
birthday: string;
sex: string;
height_m: number;
weight_kg: number;
physical_activity_level: PhysicalActivityLevel;
current_diet: string;
allergic_food: string;
chronic_disease: string;
expected_diet: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { GeneralResponse } from 'src/dto/general-response.dto';

export class UpdateCustomerProfileResponse extends GeneralResponse {}

0 comments on commit a53a50f

Please sign in to comment.