From 633ccd4906edca8ee694e66c7d9959e88623be05 Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Fri, 30 Jan 2026 14:47:46 +0700 Subject: [PATCH] fix:delete profileAvatar --- src/controllers/ProfileAvatarController.ts | 22 +++++++++++++++++++ .../ProfileAvatarEmployeeController.ts | 14 ++++++++++++ .../ProfileAvatarEmployeeTempController.ts | 14 ++++++++++++ src/interfaces/call-api.ts | 17 ++++++++++++++ 4 files changed, 67 insertions(+) diff --git a/src/controllers/ProfileAvatarController.ts b/src/controllers/ProfileAvatarController.ts index dc8a7e0f..16537b06 100644 --- a/src/controllers/ProfileAvatarController.ts +++ b/src/controllers/ProfileAvatarController.ts @@ -8,6 +8,7 @@ import { Profile } from "../entities/Profile"; import { CreateProfileAvatar, ProfileAvatar } from "../entities/ProfileAvatar"; import permission from "../interfaces/permission"; import { setLogDataDiff } from "../interfaces/utils"; +import CallAPI from "../interfaces/call-api"; @Route("api/v1/org/profile/avatar") @Tags("ProfileAvatar") @Security("bearerAuth") @@ -158,12 +159,33 @@ export class ProfileAvatarController extends Controller { "SYS_REGISTRY_OFFICER", _record.profileId, ); + if (_record.isActive) { + const profile = await this.profileRepository.findOne({ + where: { id: _record.profileId }, + }); + if (profile) { + profile.avatar = ""; + profile.avatarName = ""; + await this.profileRepository.save(profile, { data: req }); + } + } + + await new CallAPI().DeleteFile(req, `${_record?.avatar}/${_record?.avatarName}`); } if (!_record) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } + await this.avatarRepository.remove(_record, { data: req }); return new HttpSuccess(); } + + // private async deleteFile(avatar: string, avatarName: string) { + // const url = process.env.API_URL + `/salary/file/${avatar}/${avatarName}`; + // console.log(url); + // try { + // await http.delete(url); + // } catch {} + // } } diff --git a/src/controllers/ProfileAvatarEmployeeController.ts b/src/controllers/ProfileAvatarEmployeeController.ts index 21f9f536..72c652f1 100644 --- a/src/controllers/ProfileAvatarEmployeeController.ts +++ b/src/controllers/ProfileAvatarEmployeeController.ts @@ -8,6 +8,8 @@ import { CreateProfileEmployeeAvatar, ProfileAvatar } from "../entities/ProfileA import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; import { setLogDataDiff } from "../interfaces/utils"; +import CallAPI from "../interfaces/call-api"; + @Route("api/v1/org/profile-employee/avatar") @Tags("ProfileAvatar") @Security("bearerAuth") @@ -153,6 +155,18 @@ export class ProfileAvatarEmployeeController extends Controller { "SYS_REGISTRY_EMP", _record.profileEmployeeId, ); + if (_record.isActive) { + const profile = await this.profileRepository.findOne({ + where: { id: _record.profileEmployeeId }, + }); + if (profile) { + profile.avatar = ""; + profile.avatarName = ""; + await this.profileRepository.save(profile, { data: req }); + } + } + + await new CallAPI().DeleteFile(req, `${_record?.avatar}/${_record?.avatarName}`); } if (!_record) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); diff --git a/src/controllers/ProfileAvatarEmployeeTempController.ts b/src/controllers/ProfileAvatarEmployeeTempController.ts index f16944a9..e26bd53f 100644 --- a/src/controllers/ProfileAvatarEmployeeTempController.ts +++ b/src/controllers/ProfileAvatarEmployeeTempController.ts @@ -8,6 +8,7 @@ import { CreateProfileEmployeeAvatar, ProfileAvatar } from "../entities/ProfileA import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; import { setLogDataDiff } from "../interfaces/utils"; +import CallAPI from "../interfaces/call-api"; @Route("api/v1/org/profile-temp/avatar") @Tags("ProfileAvatar") @Security("bearerAuth") @@ -147,6 +148,19 @@ export class ProfileAvatarEmployeeTempController extends Controller { public async deleteAvatarEmployee(@Path() avatarId: string, @Request() req: RequestWithUser) { await new permission().PermissionDelete(req, "SYS_REGISTRY_TEMP"); const _record = await this.avatarRepository.findOneBy({ id: avatarId }); + if (_record) { + if (_record.isActive) { + const profile = await this.profileRepository.findOne({ + where: { id: _record.profileEmployeeId }, + }); + if (profile) { + profile.avatar = ""; + profile.avatarName = ""; + await this.profileRepository.save(profile, { data: req }); + } + } + } + await new CallAPI().DeleteFile(req, `${_record?.avatar}/${_record?.avatarName}`); if (!_record) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } diff --git a/src/interfaces/call-api.ts b/src/interfaces/call-api.ts index 3639c37c..398246c9 100644 --- a/src/interfaces/call-api.ts +++ b/src/interfaces/call-api.ts @@ -99,6 +99,23 @@ class CallAPI { throw error; } } + + //Delete File + public async DeleteFile(request: any, @Path() path: any) { + const token = "Bearer " + request.headers.authorization.replace("Bearer ", ""); + const url = process.env.API_URL + "/salary/file/" + path; + try { + await axios.delete(url, { + headers: { + Authorization: `${token}`, + "Content-Type": "application/json", + api_key: process.env.API_KEY, + }, + }); + } catch (error) { + throw error; + } + } } export default CallAPI;