feat: add user profile picture, region, and timezone update functionality

This commit is contained in:
faris Aljohari
2024-07-15 15:47:13 +03:00
parent 4a87a42827
commit 4fc880d869
7 changed files with 377 additions and 65 deletions
+179 -22
View File
@@ -1,28 +1,185 @@
import { Injectable } from '@nestjs/common';
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { UserListDto } from '../dtos/user.list.dto';
import { ConfigService } from '@nestjs/config';
import {
UpdateProfilePictureDataDto,
UpdateRegionDataDto,
UpdateTimezoneDataDto,
} from './../dtos/update.user.dto';
import {
BadRequestException,
HttpException,
HttpStatus,
Injectable,
} from '@nestjs/common';
import { UserRepository } from '@app/common/modules/user/repositories';
import { RegionRepository } from '@app/common/modules/region/repositories';
import { TimeZoneRepository } from '@app/common/modules/timezone/repositories';
@Injectable()
export class UserService {
private tuya: TuyaContext;
constructor(private readonly configService: ConfigService) {
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
const tuyaEuUrl = this.configService.get<string>('tuya-config.TUYA_EU_URL');
this.tuya = new TuyaContext({
baseUrl: tuyaEuUrl,
accessKey,
secretKey,
});
constructor(
private readonly userRepository: UserRepository,
private readonly regionRepository: RegionRepository,
private readonly timeZoneRepository: TimeZoneRepository,
) {}
async getUserDetailsByUserUuid(userUuid: string) {
try {
const user = await this.userRepository.findOne({
where: {
uuid: userUuid,
},
relations: ['region', 'timezone'],
});
if (!user) {
throw new BadRequestException('Invalid room UUID');
}
return {
uuid: user.uuid,
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
profilePicture: user.profilePicture,
region: user.region,
timeZone: user.timezone,
};
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
}
}
}
async userDetails(userListDto: UserListDto) {
const path = `/v2.0/apps/${userListDto.schema}/users`;
const data = await this.tuya.request({
method: 'GET',
path,
query: userListDto,
});
return data;
async updateProfilePictureByUserUuid(
userUuid: string,
updateProfilePictureDataDto: UpdateProfilePictureDataDto,
) {
try {
await this.userRepository.update(
{ uuid: userUuid },
{ ...updateProfilePictureDataDto },
);
const updatedUser = await this.getUserDetailsByUserUuid(userUuid);
return {
uuid: updatedUser.uuid,
firstName: updatedUser.firstName,
lastName: updatedUser.lastName,
profilePicture: updatedUser.profilePicture,
region: updatedUser.region,
timeZoneUuid: updatedUser.timeZone,
};
} catch (err) {
console.log('err', err);
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
}
}
}
async updateRegionByUserUuid(
userUuid: string,
updateRegionDataDto: UpdateRegionDataDto,
) {
try {
const user = await this.getUserDetailsByUserUuid(userUuid);
if (!user) {
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
}
// Ensure the region UUID is provided
if (!updateRegionDataDto.regionUuid) {
throw new BadRequestException('Region UUID is required');
}
// Ensure the region exists
const region = await this.regionRepository.findOne({
where: {
uuid: updateRegionDataDto.regionUuid,
},
});
if (!region) {
throw new BadRequestException('Invalid region UUID');
}
await this.userRepository.update(
{ uuid: userUuid },
{
region: region,
},
);
const updatedUser = await this.getUserDetailsByUserUuid(userUuid);
if (!updatedUser.region) {
throw new BadRequestException('Region update failed');
}
return {
uuid: updatedUser.uuid,
firstName: updatedUser.firstName,
lastName: updatedUser.lastName,
profilePicture: updatedUser.profilePicture,
region: updatedUser.region,
timeZoneUuid: updatedUser.timeZone,
};
} catch (err) {
throw new HttpException(
err.message || 'User not found',
HttpStatus.NOT_FOUND,
);
}
}
async updateTimezoneByUserUuid(
userUuid: string,
updateTimezoneDataDto: UpdateTimezoneDataDto,
) {
try {
const user = await this.getUserDetailsByUserUuid(userUuid);
if (!user) {
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
}
// Ensure the region UUID is provided
if (!updateTimezoneDataDto.timezoneUuid) {
throw new BadRequestException('Timezone UUID is required');
}
// Ensure the region exists
const timezone = await this.timeZoneRepository.findOne({
where: {
uuid: updateTimezoneDataDto.timezoneUuid,
},
});
if (!timezone) {
throw new BadRequestException('Invalid timezone UUID');
}
await this.userRepository.update(
{ uuid: userUuid },
{
timezone: timezone,
},
);
const updatedUser = await this.getUserDetailsByUserUuid(userUuid);
if (!updatedUser.timeZone) {
throw new BadRequestException('Timezone update failed');
}
return {
uuid: updatedUser.uuid,
firstName: updatedUser.firstName,
lastName: updatedUser.lastName,
profilePicture: updatedUser.profilePicture,
region: updatedUser.region,
timeZoneUuid: updatedUser.timeZone,
};
} catch (err) {
throw new HttpException(
err.message || 'User not found',
HttpStatus.NOT_FOUND,
);
}
}
}