update
This commit is contained in:
@ -14,14 +14,17 @@ import path from "path";
|
|||||||
import fileUpload from "express-fileupload";
|
import fileUpload from "express-fileupload";
|
||||||
import { v4 as uuidv4 } from "uuid";
|
import { v4 as uuidv4 } from "uuid";
|
||||||
import config from "../../config/stag.json";
|
import config from "../../config/stag.json";
|
||||||
import { profile } from "console";
|
|
||||||
|
|
||||||
const log: Logger<ILogObj> = new Logger({
|
const log: Logger<ILogObj> = new Logger({
|
||||||
name: "[UserController]",
|
name: "[UserController]",
|
||||||
type: "pretty",
|
type: "pretty",
|
||||||
});
|
});
|
||||||
const STORAGE_DIR = "assets/saude/";
|
const STORAGE_DIR = "assets/saude/";
|
||||||
|
|
||||||
|
const buildFileUrl = (fileName: string): string => {
|
||||||
|
const base = config.server.host_swagger.endsWith("/") ? config.server.host_swagger : `${config.server.host_swagger}/`;
|
||||||
|
return `${base}${STORAGE_DIR}${fileName}`;
|
||||||
|
};
|
||||||
|
|
||||||
export class UserController {
|
export class UserController {
|
||||||
static async list(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
static async list(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
/*
|
/*
|
||||||
@ -138,7 +141,7 @@ export class UserController {
|
|||||||
const mapped_data = list_data.map((user) => ({
|
const mapped_data = list_data.map((user) => ({
|
||||||
...user,
|
...user,
|
||||||
profile_picture: user.profile_picture ?? null,
|
profile_picture: user.profile_picture ?? null,
|
||||||
file_profile_picture: user.profile_picture ? `${config.server.host_swagger}uploads/doctor-profile/${user.profile_picture}` : null,
|
file_profile_picture: user.profile_picture ? buildFileUrl(user.profile_picture) : null,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, mapped_data);
|
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, mapped_data);
|
||||||
@ -247,7 +250,7 @@ export class UserController {
|
|||||||
const mapped_data = list_data.map((user) => ({
|
const mapped_data = list_data.map((user) => ({
|
||||||
...user,
|
...user,
|
||||||
profile_picture: user.profile_picture ?? null,
|
profile_picture: user.profile_picture ?? null,
|
||||||
file_profile_picture: user.profile_picture ? `${config.server.host_swagger}uploads/doctor-profile/${user.profile_picture}` : null,
|
file_profile_picture: user.profile_picture ? buildFileUrl(user.profile_picture) : null,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, mapped_data);
|
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, mapped_data);
|
||||||
@ -356,9 +359,9 @@ export class UserController {
|
|||||||
const mapped_data = list_data.map((user) => ({
|
const mapped_data = list_data.map((user) => ({
|
||||||
...user,
|
...user,
|
||||||
profile_picture: user.profile_picture ?? null,
|
profile_picture: user.profile_picture ?? null,
|
||||||
file_profile_picture: user.profile_picture ? `${config.server.host_swagger}uploads/doctor-profile/${user.profile_picture}` : null,
|
file_profile_picture: user.profile_picture ? buildFileUrl(user.profile_picture) : null,
|
||||||
signature: user.signature ?? null,
|
signature: user.signature ?? null,
|
||||||
file_signature: user.signature ? `${config.server.host_swagger}uploads/user-signature/${user.signature}` : null,
|
file_signature: user.signature ? buildFileUrl(user.signature) : null,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, mapped_data);
|
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, mapped_data);
|
||||||
@ -782,7 +785,7 @@ export class UserController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async uploadFile(files: fileUpload.FileArray | null | undefined, fieldName: string, folderPath: string): Promise<string | null> {
|
static async uploadFile(files: fileUpload.FileArray | null | undefined, fieldName: string): Promise<string | null> {
|
||||||
if (!files || !files[fieldName]) return null;
|
if (!files || !files[fieldName]) return null;
|
||||||
|
|
||||||
const file = files[fieldName] as fileUpload.UploadedFile;
|
const file = files[fieldName] as fileUpload.UploadedFile;
|
||||||
@ -792,10 +795,10 @@ export class UserController {
|
|||||||
throw { message: `${fieldName}: File size exceeds 1MB limit` };
|
throw { message: `${fieldName}: File size exceeds 1MB limit` };
|
||||||
}
|
}
|
||||||
|
|
||||||
const allowedMimes = ["image/jpeg", "image/jpg", "image/png", "image/webp"];
|
const allowedMimes = ["image/jpeg", "image/jpg", "image/png", "image/webp", "image/svg+xml", "image/svg"];
|
||||||
if (!allowedMimes.includes(file.mimetype)) {
|
if (!allowedMimes.includes(file.mimetype)) {
|
||||||
throw {
|
throw {
|
||||||
message: `${fieldName}: Invalid file type. Only JPG, PNG, and WEBP are allowed`,
|
message: `${fieldName}: Invalid file type. Only JPG, PNG, WEBP, and SVG are allowed`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -879,7 +882,7 @@ export class UserController {
|
|||||||
const data = await userRepository.findOneBy({ id: param.id });
|
const data = await userRepository.findOneBy({ id: param.id });
|
||||||
|
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
const doctorprofile = await UserController.uploadFile(req.files, "", STORAGE_DIR);
|
const doctorprofile = await UserController.uploadFile(req.files, "");
|
||||||
if (doctorprofile) {
|
if (doctorprofile) {
|
||||||
if (data.profile_picture) {
|
if (data.profile_picture) {
|
||||||
const oldFilePath = path.join(STORAGE_DIR, data.profile_picture);
|
const oldFilePath = path.join(STORAGE_DIR, data.profile_picture);
|
||||||
@ -973,7 +976,7 @@ export class UserController {
|
|||||||
const data = await userRepository.findOneBy({ id: param.id });
|
const data = await userRepository.findOneBy({ id: param.id });
|
||||||
|
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
const usersignature = await UserController.uploadFile(req.files, "user_signature", STORAGE_DIR);
|
const usersignature = await UserController.uploadFile(req.files, "user_signature");
|
||||||
|
|
||||||
if (usersignature) {
|
if (usersignature) {
|
||||||
// ← Hapus signature lama jika ada
|
// ← Hapus signature lama jika ada
|
||||||
@ -1097,9 +1100,9 @@ export class UserController {
|
|||||||
const data = {
|
const data = {
|
||||||
...detail,
|
...detail,
|
||||||
signature: detail.signature ?? null,
|
signature: detail.signature ?? null,
|
||||||
file_signature: detail.signature ? `${config.server.host_swagger}uploads/user-signature/${detail.signature}` : null,
|
file_signature: detail.signature ? buildFileUrl(detail.signature) : null,
|
||||||
profile: detail.profile_picture ?? null,
|
profile: detail.profile_picture ?? null,
|
||||||
profile_picture: detail.profile_picture ? `${config.server.host_swagger}uploads/doctor-profile/${detail.profile_picture}` : null,
|
profile_picture: detail.profile_picture ? buildFileUrl(detail.profile_picture) : null,
|
||||||
};
|
};
|
||||||
|
|
||||||
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, data);
|
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, data);
|
||||||
|
|||||||
@ -16,6 +16,7 @@ export class RoutePublic {
|
|||||||
app.put('/api/logout/:refresh_token', AuthController.logout)
|
app.put('/api/logout/:refresh_token', AuthController.logout)
|
||||||
app.put('/api/renew_token/:refresh_token/:application', AuthController.renewToken)
|
app.put('/api/renew_token/:refresh_token/:application', AuthController.renewToken)
|
||||||
app.use('/static', express.static(path.join(__dirname, 'assets')));
|
app.use('/static', express.static(path.join(__dirname, 'assets')));
|
||||||
|
app.use('/assets', express.static(path.join(process.cwd(), 'assets')));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user