update: add signature and profile
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,3 +9,4 @@ package-lock.json
|
||||
src/swagger/swagger.json
|
||||
swagger.json
|
||||
config/ferro.json
|
||||
uploads/*
|
||||
@ -27,8 +27,10 @@
|
||||
"entity": "file:../entity",
|
||||
"exceljs": "^4.4.0",
|
||||
"express": "^4.21.1",
|
||||
"express-fileupload": "^1.5.2",
|
||||
"express-jwt": "^8.4.1",
|
||||
"fast-csv": "^5.0.2",
|
||||
"fileupload": "^1.0.0",
|
||||
"helmet": "^8.0.0",
|
||||
"i": "^0.3.7",
|
||||
"joi": "^17.13.3",
|
||||
@ -53,6 +55,7 @@
|
||||
"@types/config": "^3.3.5",
|
||||
"@types/cors": "^2.8.17",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/express-fileupload": "^1.5.1",
|
||||
"@types/morgan": "^1.9.9",
|
||||
"@types/swagger-ui-express": "^4.1.7"
|
||||
}
|
||||
|
||||
@ -270,7 +270,7 @@ export class AuthController {
|
||||
|
||||
const repo = OrmHelper.DB.getRepository(UserRefreshToken)
|
||||
|
||||
const affected = (await repo.delete({ refresh_token: param.refresh_token })).affected
|
||||
const affected = (await repo.delete({ refresh_token: param.refresh_token })).affected ?? 0
|
||||
|
||||
if (affected > 0) {
|
||||
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_logout)
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import axios from "axios";
|
||||
import config from "config";
|
||||
import { HrmsEmployee, Paging, User, UserRefreshToken, UserRole } from "entity";
|
||||
import exceljs from "exceljs";
|
||||
import { NextFunction, Response } from "express";
|
||||
@ -10,6 +9,12 @@ import CommonHelper from "../helpers/common";
|
||||
import { ReturnHelper } from "../helpers/express/return";
|
||||
import { OrmHelper } from "../helpers/orm";
|
||||
import { Language } from "../langs/lang";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import fileUpload from "express-fileupload";
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import config from '../../config/stag.json';
|
||||
import { profile } from "console";
|
||||
|
||||
const log: Logger<ILogObj> = new Logger({
|
||||
name: "[UserController]",
|
||||
@ -128,6 +133,15 @@ export class UserController {
|
||||
const list_data = await res_list.getMany();
|
||||
const count_data = CommonHelper.countObject(list_data);
|
||||
|
||||
// ← Tambahkan mapping ini
|
||||
const mapped_data = list_data.map((user) => ({
|
||||
...user,
|
||||
profile_picture: user.profile_picture ?? null,
|
||||
file_profile_picture: user.profile_picture
|
||||
? `${config.server.host_swagger}uploads/profile-picture/${user.profile_picture}`
|
||||
: null,
|
||||
}));
|
||||
|
||||
return ReturnHelper.successResponselist(
|
||||
res,
|
||||
200,
|
||||
@ -135,7 +149,7 @@ export class UserController {
|
||||
count_data,
|
||||
current_page,
|
||||
total_count_data,
|
||||
list_data,
|
||||
mapped_data,
|
||||
);
|
||||
} catch (e: unknown) {
|
||||
log.error(e);
|
||||
@ -308,7 +322,7 @@ export class UserController {
|
||||
}
|
||||
|
||||
data.hashPassword();
|
||||
data.created_by = req.auth.data.name;
|
||||
data.created_by = req.auth?.data.name;
|
||||
data.created_at = new Date();
|
||||
|
||||
await OrmHelper.DB.manager.save(data);
|
||||
@ -382,7 +396,7 @@ export class UserController {
|
||||
);
|
||||
}
|
||||
|
||||
if (r.application.id == new_role.application.id) {
|
||||
if (r.application.id == new_role?.application.id) {
|
||||
return ReturnHelper.errorResponse(
|
||||
res,
|
||||
409,
|
||||
@ -519,7 +533,7 @@ export class UserController {
|
||||
name: Joi.string().max(64).required().label("Name"),
|
||||
});
|
||||
|
||||
req.body.id = req.auth.data.id;
|
||||
req.body.id = req.auth?.data.id;
|
||||
|
||||
const param: User = await schema.validateAsync(req.body);
|
||||
|
||||
@ -531,7 +545,7 @@ export class UserController {
|
||||
data.name = param.name;
|
||||
data.username = param.username;
|
||||
data.email = param.email;
|
||||
data.updated_by = req.auth.data.name;
|
||||
data.updated_by = req.auth?.data.name;
|
||||
|
||||
await userRepository.save(data);
|
||||
|
||||
@ -578,7 +592,7 @@ export class UserController {
|
||||
retype_password: Joi.ref("password"),
|
||||
});
|
||||
|
||||
req.body.id = req.auth.data.id;
|
||||
req.body.id = req.auth?.data.id;
|
||||
|
||||
const param: User = await schema.validateAsync(req.body);
|
||||
|
||||
@ -606,12 +620,57 @@ export class UserController {
|
||||
}
|
||||
}
|
||||
|
||||
static async uploadFile(
|
||||
files: fileUpload.FileArray | null | undefined,
|
||||
fieldName: string,
|
||||
folderPath: string
|
||||
): Promise<string | null> {
|
||||
if (!files || !files[fieldName]) return null;
|
||||
|
||||
const file = files[fieldName] as fileUpload.UploadedFile;
|
||||
|
||||
const maxSize = 1 * 1024 * 1024; // 1MB
|
||||
if (file.size > maxSize) {
|
||||
throw { message: `${fieldName}: File size exceeds 1MB limit` };
|
||||
}
|
||||
|
||||
const allowedMimes = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp'];
|
||||
if (!allowedMimes.includes(file.mimetype)) {
|
||||
throw { message: `${fieldName}: Invalid file type. Only JPG, PNG, and WEBP are allowed` };
|
||||
}
|
||||
|
||||
const ext = file.name.split('.');
|
||||
const name = uuidv4() + '.' + ext[ext.length - 1];
|
||||
const file_path = path.join(folderPath, name);
|
||||
|
||||
if (!fs.existsSync(folderPath)) {
|
||||
fs.mkdirSync(folderPath, { recursive: true });
|
||||
}
|
||||
|
||||
await file.mv(file_path);
|
||||
log.info(`File uploaded successfully: ${file_path}`);
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
static async deleteFile(filePath: string): Promise<void> {
|
||||
try {
|
||||
if (fs.existsSync(filePath)) {
|
||||
fs.unlinkSync(filePath);
|
||||
log.info(`File deleted successfully: ${filePath}`);
|
||||
}
|
||||
} catch (e) {
|
||||
log.error(`Failed to delete file: ${filePath}`, e);
|
||||
}
|
||||
}
|
||||
|
||||
static async update(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||
/*
|
||||
#swagger.tags = ['User']
|
||||
#swagger.security = [{
|
||||
"bearerAuth": []
|
||||
}]
|
||||
#swagger.consumes = ['multipart/form-data']
|
||||
#swagger.parameters['id'] = {
|
||||
in: 'path',
|
||||
description: 'User ID.',
|
||||
@ -622,9 +681,17 @@ export class UserController {
|
||||
#swagger.requestBody = {
|
||||
required: true,
|
||||
content: {
|
||||
"application/json": {
|
||||
"multipart/form-data": {
|
||||
schema: {
|
||||
$ref: "#/components/schemas/user"
|
||||
type: "object",
|
||||
properties: {
|
||||
email: { type: "string", description: "Email" },
|
||||
username: { type: "string", description: "Username" },
|
||||
password: { type: "string", description: "Password" },
|
||||
name: { type: "string", description: "name" },
|
||||
status: { type: "string", description: "Status" },
|
||||
profile_picture: { type: "string", format: "binary", description: "Image file (max 1MB, jpg/png/webp)" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -637,10 +704,10 @@ export class UserController {
|
||||
email: Joi.string().email().max(64).required().label("Email"),
|
||||
username: Joi.string().max(64).required().label("Username"),
|
||||
password: Joi.string().allow("").optional().label("Password"),
|
||||
retype_password: Joi.ref("password"),
|
||||
name: Joi.string().max(64).required().label("Name"),
|
||||
employee_id: Joi.string().uuid().optional().allow("").label("Employee ID"),
|
||||
status: Joi.string().required().label("Status"),
|
||||
profile_picture: Joi.string().allow('', null).optional().label("Profile Picture"),
|
||||
});
|
||||
|
||||
req.body.id = req.params["id"];
|
||||
@ -652,11 +719,20 @@ export class UserController {
|
||||
const data = await userRepository.findOneBy({ id: param.id });
|
||||
|
||||
if (data != null) {
|
||||
const doctorprofile = await UserController.uploadFile(req.files, 'profile_picture', 'uploads/profile-picture');
|
||||
if (doctorprofile) {
|
||||
if (data.profile_picture) {
|
||||
const oldFilePath = path.join('uploads/profile-picture', data.profile_picture);
|
||||
await UserController.deleteFile(oldFilePath);
|
||||
}
|
||||
data.profile_picture = doctorprofile;
|
||||
}
|
||||
|
||||
data.name = param.name;
|
||||
data.username = param.username;
|
||||
data.email = param.email;
|
||||
data.status = param.status;
|
||||
data.updated_by = req.auth.data.name;
|
||||
data.updated_by = req.auth?.data.name;
|
||||
data.updated_at = new Date();
|
||||
|
||||
// if (param.employee_id && param.employee_id != "") {
|
||||
@ -680,6 +756,104 @@ export class UserController {
|
||||
return ReturnHelper.errorResponse(res, 404, 401, Language.lang.failed_not_found, "");
|
||||
}
|
||||
} catch (e: unknown) {
|
||||
// ← Ganti dari log.error(e) menjadi ini
|
||||
if (e instanceof Error) {
|
||||
log.error(e.message, e.stack);
|
||||
} else {
|
||||
log.error(JSON.stringify(e));
|
||||
}
|
||||
const err = e as Error;
|
||||
|
||||
return ReturnHelper.errorResponse(res, 500, 402, Language.lang.failed_update, err.message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static async updateUserSignature(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||
/*
|
||||
#swagger.tags = ['User']
|
||||
#swagger.security = [{
|
||||
"bearerAuth": []
|
||||
}]
|
||||
#swagger.consumes = ['multipart/form-data']
|
||||
#swagger.parameters['id'] = {
|
||||
in: 'path',
|
||||
description: 'User ID.',
|
||||
required: true,
|
||||
type: 'string'
|
||||
}
|
||||
|
||||
#swagger.requestBody = {
|
||||
required: true,
|
||||
content: {
|
||||
"multipart/form-data": {
|
||||
schema: {
|
||||
type: "object",
|
||||
required: ["user_signature"],
|
||||
properties: {
|
||||
user_signature: { type: "string", format: "binary", description: "Image file (max 1MB, jpg/png/webp)" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
try {
|
||||
const schema = Joi.object().keys({
|
||||
id: Joi.string().uuid().required().label("ID"),
|
||||
user_signature: Joi.string().allow('', null).optional().label("User Signature"),
|
||||
});
|
||||
|
||||
req.body.id = req.params["id"];
|
||||
|
||||
const param: any = await schema.validateAsync(req.body);
|
||||
|
||||
const userRepository = OrmHelper.DB.getRepository(User);
|
||||
|
||||
const data = await userRepository.findOneBy({ id: param.id });
|
||||
|
||||
if (data != null) {
|
||||
const usersignature = await UserController.uploadFile(req.files, 'user_signature', 'uploads/user-signature');
|
||||
|
||||
if (usersignature) {
|
||||
// ← Hapus signature lama jika ada
|
||||
if (data.signature) {
|
||||
const oldFilePath = path.join('uploads/user-signature', data.signature);
|
||||
await UserController.deleteFile(oldFilePath);
|
||||
}
|
||||
|
||||
data.signature = usersignature;
|
||||
}
|
||||
|
||||
// data.name = param.name;
|
||||
// data.username = param.username;
|
||||
// data.email = param.email;
|
||||
// data.status = param.status;
|
||||
// data.updated_by = req.auth?.data.name;
|
||||
// data.updated_at = new Date();
|
||||
|
||||
// if (param.employee_id && param.employee_id != "") {
|
||||
// const employee = await OrmHelper.DB.manager
|
||||
// .getRepository(HrmsEmployee)
|
||||
// .findOneByOrFail({id: param.employee_id});
|
||||
// data.employee = employee;
|
||||
// } else {
|
||||
// data.employee = null;
|
||||
// }
|
||||
|
||||
// if (param.password) {
|
||||
// data.password = param.password;
|
||||
// data.hashPassword();
|
||||
// }
|
||||
|
||||
await userRepository.save(data);
|
||||
|
||||
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_update, data);
|
||||
} else {
|
||||
return ReturnHelper.errorResponse(res, 404, 401, Language.lang.failed_not_found, "");
|
||||
}
|
||||
} catch (e: unknown) {
|
||||
log.error(e);
|
||||
const err = e as Error;
|
||||
|
||||
@ -721,7 +895,7 @@ export class UserController {
|
||||
!param.hard
|
||||
? await userRepository.softDelete({ id: param.id })
|
||||
: await userRepository.delete({ id: param.id })
|
||||
).affected;
|
||||
).affected ?? 0;
|
||||
const affected_rt = (
|
||||
!param.hard
|
||||
? await userRTRepository.softDelete({ id_user: param.id })
|
||||
@ -780,11 +954,19 @@ export class UserController {
|
||||
where: { id: param.id },
|
||||
});
|
||||
|
||||
if (detail != null) {
|
||||
const data = {
|
||||
...detail,
|
||||
signature: detail.signature ?? null,
|
||||
file_signature: detail.signature
|
||||
? `${config.server.host_swagger}uploads/user-signature/${detail.signature}`
|
||||
: null,
|
||||
profile: detail.profile_picture ?? null,
|
||||
profile_picture: detail.profile_picture
|
||||
? `${config.server.host_swagger}uploads/doctor-profile/${detail.profile_picture}`
|
||||
: null,
|
||||
};
|
||||
|
||||
if (data != null) {
|
||||
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, data);
|
||||
} else {
|
||||
return ReturnHelper.errorResponse(res, 404, 401, Language.lang.failed_not_found, "");
|
||||
@ -821,7 +1003,7 @@ export class UserController {
|
||||
const userRepository = OrmHelper.DB.getRepository(User);
|
||||
const userRTRepository = OrmHelper.DB.getRepository(UserRefreshToken);
|
||||
|
||||
const affected = (await userRepository.restore({id: param.id})).affected;
|
||||
const affected = (await userRepository.restore({ id: param.id })).affected ?? 0;
|
||||
const affected_rt = (await userRTRepository.restore({ id_user: param.id })).affected;
|
||||
|
||||
if (affected > 0) {
|
||||
|
||||
12
src/helpers/express/upload.ts
Normal file
12
src/helpers/express/upload.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import config from 'config';
|
||||
import express from 'express';
|
||||
import fileUpload from 'express-fileupload'
|
||||
|
||||
export class UploadHelper {
|
||||
static setup(app: express.Application) {
|
||||
app.use(fileUpload({
|
||||
limits: { fileSize: 10485760 },
|
||||
abortOnLimit: true,
|
||||
}));
|
||||
}
|
||||
}
|
||||
@ -2,51 +2,119 @@ import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { NextFunction, Request, Response } from 'express';
|
||||
|
||||
interface LanguageValue {
|
||||
success_insert?: string;
|
||||
success_update?: string;
|
||||
success_delete?: string;
|
||||
success_restore?: string;
|
||||
success_view?: string;
|
||||
success?: string;
|
||||
success_valid_otp?: string;
|
||||
success_logout?: string;
|
||||
// interface LanguageValue {
|
||||
// success_insert?: string;
|
||||
// success_update?: string;
|
||||
// success_delete?: string;
|
||||
// success_restore?: string;
|
||||
// success_view?: string;
|
||||
// success?: string;
|
||||
// success_valid_otp?: string;
|
||||
// success_logout?: string;
|
||||
|
||||
failed?: string;
|
||||
failed_access?: string;
|
||||
failed_access_otp?: string;
|
||||
failed_expired_otp?: string;
|
||||
failed_access_pin?: string;
|
||||
failed_try_pin?: string;
|
||||
failed_insert?: string;
|
||||
failed_update?: string;
|
||||
failed_delete?: string;
|
||||
failed_restore?: string;
|
||||
failed_save?: string;
|
||||
failed_view?: string;
|
||||
failed_empty?: string;
|
||||
failed_data?: string;
|
||||
failed_duplicate?: string;
|
||||
failed_not_found?: string;
|
||||
failed_limit_otp?: string;
|
||||
failed_limit_link_code?: string;
|
||||
failed_expired_token?: string;
|
||||
failed_token?: string;
|
||||
failed_add_point?: string;
|
||||
// failed?: string;
|
||||
// failed_access?: string;
|
||||
// failed_access_otp?: string;
|
||||
// failed_expired_otp?: string;
|
||||
// failed_access_pin?: string;
|
||||
// failed_try_pin?: string;
|
||||
// failed_insert?: string;
|
||||
// failed_update?: string;
|
||||
// failed_delete?: string;
|
||||
// failed_restore?: string;
|
||||
// failed_save?: string;
|
||||
// failed_view?: string;
|
||||
// failed_empty?: string;
|
||||
// failed_data?: string;
|
||||
// failed_duplicate?: string;
|
||||
// failed_not_found?: string;
|
||||
// failed_limit_otp?: string;
|
||||
// failed_limit_link_code?: string;
|
||||
// failed_expired_token?: string;
|
||||
// failed_token?: string;
|
||||
// failed_add_point?: string;
|
||||
// failed_logout?: string;
|
||||
// failed_related?: string;
|
||||
|
||||
// //login
|
||||
// success_login?: string;
|
||||
// failed_password?: string;
|
||||
// }
|
||||
|
||||
// export class Language {
|
||||
// static lang_init: LanguageValue = {};
|
||||
// static lang: LanguageValue = {};
|
||||
|
||||
// static setup() {
|
||||
// const fileLang: any = fs.readFileSync(
|
||||
// path.join(__dirname, '../langs/json/en.json')
|
||||
// );
|
||||
// Language.lang_init['en'] = JSON.parse(fileLang.toString('utf8'));
|
||||
// }
|
||||
|
||||
// static apply = (req: Request, res: Response, next: NextFunction) => {
|
||||
// const acceptLanguageHeader = req.get('Accept-Language') as string | null;
|
||||
// if (!acceptLanguageHeader) {
|
||||
// //default
|
||||
// req.params.language = 'en';
|
||||
// } else {
|
||||
// req.params.language = acceptLanguageHeader.substring(0, 2).toLowerCase();
|
||||
// }
|
||||
|
||||
// Language.lang = Language.lang_init[req.params.language] ? Language.lang_init[req.params.language] : Language.lang_init['en'];
|
||||
|
||||
// return next();
|
||||
// }
|
||||
// }
|
||||
|
||||
interface LanguageValue {
|
||||
success_insert: string;
|
||||
success_update: string;
|
||||
success_delete: string;
|
||||
success_restore: string;
|
||||
success_view: string;
|
||||
success: string;
|
||||
success_valid_otp: string;
|
||||
|
||||
failed: string;
|
||||
failed_access: string;
|
||||
failed_access_otp: string;
|
||||
failed_expired_otp: string;
|
||||
failed_access_pin: string;
|
||||
failed_try_pin: string;
|
||||
failed_insert: string;
|
||||
failed_update: string;
|
||||
failed_delete: string;
|
||||
failed_restore: string;
|
||||
failed_save: string;
|
||||
failed_view: string;
|
||||
failed_empty: string;
|
||||
failed_data: string;
|
||||
failed_duplicate: string;
|
||||
failed_not_found: string;
|
||||
failed_limit_otp: string;
|
||||
failed_limit_link_code: string;
|
||||
failed_expired_token: string;
|
||||
failed_token: string;
|
||||
failed_add_point: string;
|
||||
success_login: string;
|
||||
failed_password: string;
|
||||
success_logout?: string;
|
||||
failed_logout?: string;
|
||||
failed_related?: string;
|
||||
}
|
||||
|
||||
//login
|
||||
success_login?: string;
|
||||
failed_password?: string;
|
||||
// Tambah index signature agar bisa akses dengan ['en']
|
||||
interface LanguageStore {
|
||||
[key: string]: LanguageValue;
|
||||
}
|
||||
|
||||
export class Language {
|
||||
static lang_init: LanguageValue = {};
|
||||
static lang: LanguageValue = {};
|
||||
static lang_init: LanguageStore = {};
|
||||
static lang: LanguageValue = {} as LanguageValue;
|
||||
|
||||
static setup() {
|
||||
const fileLang: any = fs.readFileSync(
|
||||
const fileLang = fs.readFileSync(
|
||||
path.join(__dirname, '../langs/json/en.json')
|
||||
);
|
||||
Language.lang_init['en'] = JSON.parse(fileLang.toString('utf8'));
|
||||
@ -55,13 +123,13 @@ export class Language {
|
||||
static apply = (req: Request, res: Response, next: NextFunction) => {
|
||||
const acceptLanguageHeader = req.get('Accept-Language') as string | null;
|
||||
if (!acceptLanguageHeader) {
|
||||
//default
|
||||
req.params.language = 'en';
|
||||
} else {
|
||||
req.params.language = acceptLanguageHeader.substring(0, 2).toLowerCase();
|
||||
}
|
||||
|
||||
Language.lang = Language.lang_init[req.params.language] ? Language.lang_init[req.params.language] : Language.lang_init['en'];
|
||||
Language.lang = Language.lang_init[req.params.language]
|
||||
?? Language.lang_init['en'];
|
||||
|
||||
return next();
|
||||
}
|
||||
|
||||
@ -10,9 +10,11 @@ import { RoutePublic } from './routes/public';
|
||||
import { Language } from './langs/lang';
|
||||
import { SwaggerHelper } from './helpers/express/swagger';
|
||||
import { TrustProxyHelper } from './helpers/express/trust_proxy';
|
||||
import { UploadHelper } from './helpers/express/upload'
|
||||
|
||||
const app = express();
|
||||
|
||||
UploadHelper.setup(app)
|
||||
CorsHelper.setup(app);
|
||||
CompressionHelper.setup(app);
|
||||
MorganHelper.setup(app);
|
||||
|
||||
Reference in New Issue
Block a user