add api sync-missing-emptype
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m4s

This commit is contained in:
Warunee Tamkoo 2026-04-28 11:38:47 +07:00
parent b5fb2346ab
commit 2417c90dc2
2 changed files with 294 additions and 0 deletions

View file

@ -315,4 +315,81 @@ export class KeycloakSyncController extends Controller {
...result,
});
}
/**
* Sync profiles with missing empType for a specific month (Admin only)
*
* @summary Find profiles updated in specified month with missing empType in Keycloak and sync them (ADMIN)
*
* @description
* This endpoint will:
* - List profiles from Profile table where lastUpdatedAt falls within the specified month
* - For each profile, check Keycloak if empType attribute is empty/null
* - If empType is empty, sync the profile using existing sync logic
* - Return summary of sync results
*
* Features:
* - Dry run mode (dryRun=true) to check without syncing
* - Configurable concurrency for parallel processing
* - Rate limiting to avoid overwhelming Keycloak
* - Detailed error reporting
* - Idempotent (can be safely re-run)
*
* @param {request} request Request body containing month parameter
* @param dryRun - If true, only check without syncing (default: false)
* @param concurrency - Number of parallel operations (default: 5)
* @param rateLimit - Requests per second limit (default: 10)
*/
@Post("sync-missing-emptype")
@Response<HttpError>(HttpStatus.BAD_REQUEST, "Invalid month format")
@Response<HttpError>(HttpStatus.INTERNAL_SERVER_ERROR, "Sync operation failed")
async syncMissingEmpType(
@Body() request: {
month: string;
profileType?: "PROFILE" | "PROFILE_EMPLOYEE";
},
@Query() dryRun: boolean = false,
@Query() concurrency: number = 5,
@Query() rateLimit: number = 10,
) {
const { month, profileType = "PROFILE" } = request;
// Validate month format (YYYY-MM)
const monthRegex = /^\d{4}-\d{2}$/;
if (!monthRegex.test(month)) {
throw new HttpError(HttpStatus.BAD_REQUEST, "รูปแบบเดือนไม่ถูกต้อง ต้องเป็น YYYY-MM");
}
// Validate profileType
if (!["PROFILE", "PROFILE_EMPLOYEE"].includes(profileType)) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"profileType ต้องเป็น PROFILE หรือ PROFILE_EMPLOYEE เท่านั้น",
);
}
// Validate concurrency
if (concurrency < 1 || concurrency > 20) {
throw new HttpError(HttpStatus.BAD_REQUEST, "concurrency ต้องอยู่ระหว่าง 1 ถึง 20");
}
// Validate rateLimit
if (rateLimit < 1 || rateLimit > 50) {
throw new HttpError(HttpStatus.BAD_REQUEST, "rateLimit ต้องอยู่ระหว่าง 1 ถึง 50");
}
// Execute sync
const result = await this.keycloakAttributeService.syncMissingEmpTypeByMonth({
month,
profileType,
dryRun,
concurrency,
rateLimit,
});
return new HttpSuccess({
message: `Sync ${dryRun ? "check " : ""}เสร็จสิ้น`,
...result,
});
}
}