Commit a6e8f323 by Sirasit.y

mylearn : create company for external

#50274: ระบบ MyPortal-Mylearn ปรับการเพิ่มผู้ใช้สิทธิ์ Adminโดยระบบ MyPortal
parent 1cab92fb
...@@ -58,7 +58,8 @@ ...@@ -58,7 +58,8 @@
<th scope="col" class="text-start">{{"ชื่อบริษัท"|translate}}</th> <th scope="col" class="text-start">{{"ชื่อบริษัท"|translate}}</th>
<th scope="col" class="text-start">{{"หน่วยงาน" | translate}}</th> <th scope="col" class="text-start">{{"หน่วยงาน" | translate}}</th>
<th scope="col" class="text-start">{{"แหล่งที่มาบริษัท" | translate}}</th> <th scope="col" class="text-start">{{"แหล่งที่มาบริษัท" | translate}}</th>
<th scope="col" class="text-start">{{"การจัดการคอร์ส"|translate}}</th> <th scope="col" class="text-start">{{"จัดการข้อมูล" | translate}}</th>
<!-- <th scope="col" class="text-start">{{"การจัดการคอร์ส"|translate}}</th> -->
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
...@@ -109,16 +110,21 @@ ...@@ -109,16 +110,21 @@
</td> </td>
<td> <td>
<div class="flex flex-row items-center !gap-2 "> <div class="flex flex-row items-center !gap-2 ">
<a aria-label="anchor" (click)="viewHrcompanyModel(item)" data-hs-overlay="#modal-detail"
class="ti-btn ti-btn-wave !gap-0 !m-0 bg-info/10 text-info hover:bg-info hover:text-white hover:border-info">
<i class="ri-pencil-line"></i>
</a>
</div>
</td>
<!-- ให้ไปใช้เมนูหน้า จัดการคอร์ส-จัดการคอร์สบริษัท แทน -->
<!-- <td>
<div class="flex flex-row items-center !gap-2 ">
<a aria-label="anchor" (click)="goToDetail(item.companyId)" <a aria-label="anchor" (click)="goToDetail(item.companyId)"
class="ti-btn ti-btn-wave !gap-0 !m-0 bg-info/10 text-info hover:bg-info hover:text-white hover:border-info cursor-pointer"> class="ti-btn ti-btn-wave !gap-0 !m-0 bg-info/10 text-info hover:bg-info hover:text-white hover:border-info cursor-pointer">
<i class="ri-eye-line"></i> <i class="ri-eye-line"></i>
</a> </a>
<!-- <a aria-label="anchor" href="javascript:void(0);"
class="ti-btn ti-btn-wave product-btn !gap-0 !m-0 bg-danger/10 text-danger hover:bg-danger hover:text-white hover:border-danger">
<i class="ri-delete-bin-line"></i>
</a> -->
</div> </div>
</td> </td> -->
</tr> </tr>
} }
} @else { } @else {
...@@ -217,7 +223,7 @@ ...@@ -217,7 +223,7 @@
</div> </div>
</div> </div>
<div class="xl:col-span-12 col-span-12 my-2" *ngIf="statusHrcompanyModel=='add'"> <!-- <div class="xl:col-span-12 col-span-12 my-2" *ngIf="statusHrcompanyModel=='add'">
<label for="deal-title" class="form-label">{{ 'อีเมลบริษัท' | translate}}</label> <label for="deal-title" class="form-label">{{ 'อีเมลบริษัท' | translate}}</label>
<input type="text" <input type="text"
...@@ -235,7 +241,29 @@ ...@@ -235,7 +241,29 @@
{{ 'Please fill in information' | translate }} {{ 'Please fill in information' | translate }}
</div> </div>
<div class="text-danger" *ngIf="companyEmailCtrl.errors?.['pattern']"> <div class="text-danger" *ngIf="companyEmailCtrl.errors?.['pattern']">
{{ 'อีเมลไม่ต้องถูก' | translate }} {{ 'รูปแบบอีเมลไม่ถูกต้อง' | translate }}
</div>
</div> -->
<div class="xl:col-span-12 col-span-12 my-2" *ngIf="statusHrcompanyModel=='add'">
<label for="deal-title" class="form-label">{{ 'อีเมลบริษัทสำหรับ users' | translate}}</label>
<input type="text"
class="form-control"
id="deal-title"
name="companyEmail"
placeholder=""
[(ngModel)]="selectHrcompanyModel.companyEmail"
(blur)="onCheckCompanyEmail()">
<div class="text-danger" *ngIf="validateEmail === 'required'">
{{ 'Please fill in information' | translate }}
</div>
<div class="text-danger" *ngIf="validateEmail === 'pattern'">
{{ 'รูปแบบอีเมลไม่ถูกต้อง' | translate }}
</div>
<div class="text-danger" *ngIf="validateEmail === 'un-usable'">
{{ 'ไม่สามารถใช้อีเมลได้เนื่องจากมีข้อมูล user ซ้ำกันกับท่านอื่น' | translate }}
</div> </div>
</div> </div>
...@@ -277,8 +305,12 @@ ...@@ -277,8 +305,12 @@
{{'Cancel' | translate}} {{'Cancel' | translate}}
</button> </button>
<button type="button" (click)="saveHrcompanyModel()" class="ti-btn bg-primary text-white !font-medium" <button type="button" (click)="saveHrcompanyModel()" class="ti-btn bg-primary text-white !font-medium"
[class.ti-btn-disabled]="!selectHrcompanyModel.companyCode||!selectHrcompanyModel.companyName" [class.ti-btn-disabled]="!selectHrcompanyModel.companyCode || !selectHrcompanyModel.companyName || !selectHrcompanyModel.companyEmail|| (validateEmail !== 'usable')"
[disabled]="!selectHrcompanyModel.companyCode||!selectHrcompanyModel.companyName">{{'Save' | translate}}</button> [disabled]="!selectHrcompanyModel.companyCode || !selectHrcompanyModel.companyName || !selectHrcompanyModel.companyEmail || (validateEmail !== 'usable')">{{'บันทึกข้อมูลบริษัท' | translate}}</button>
<button type="button" (click)="generateUsersAdmin(selectHrcompanyModel.companyId)" class="ti-btn bg-primary text-white !font-medium"
[class.ti-btn-disabled]="!selectHrcompanyModel.companyCode || !selectHrcompanyModel.companyName || !selectHrcompanyModel.companyEmail|| (validateEmail !== 'usable') || !isEnabledBtnAdmin"
[disabled]="!selectHrcompanyModel.companyCode || !selectHrcompanyModel.companyName || !selectHrcompanyModel.companyEmail || (validateEmail !== 'usable') || !isEnabledBtnAdmin">{{'สร้างข้อมูลแอดมินบริษัท' | translate}}</button>
</div> </div>
</div> </div>
</div> </div>
...@@ -2,6 +2,7 @@ import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; ...@@ -2,6 +2,7 @@ import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { SharedModule } from '../../../shared/shared.module'; import { SharedModule } from '../../../shared/shared.module';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { TranslateModule, TranslateService } from '@ngx-translate/core'; import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { UserService } from '../../services/mylearn/user.service';
import { import {
HrcompanyModel, HrcompanyModel,
MyHrcompanyModel, MyHrcompanyModel,
...@@ -10,6 +11,7 @@ import { MyhrcompanyService } from '../../services/mylearn/myhrcompany.service'; ...@@ -10,6 +11,7 @@ import { MyhrcompanyService } from '../../services/mylearn/myhrcompany.service';
import swal from 'sweetalert'; import swal from 'sweetalert';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { ResponseModel } from '../../models/response.model.';
@Component({ @Component({
selector: 'app-myhrcompany', selector: 'app-myhrcompany',
...@@ -27,6 +29,8 @@ export class MyhrcompanyComponent implements OnInit { ...@@ -27,6 +29,8 @@ export class MyhrcompanyComponent implements OnInit {
@ViewChild('modalDetail') public modalDetail?: ElementRef; @ViewChild('modalDetail') public modalDetail?: ElementRef;
selectHrcompanyModel: HrcompanyModel = new MyHrcompanyModel(); selectHrcompanyModel: HrcompanyModel = new MyHrcompanyModel();
statusHrcompanyModel: 'add' | 'edit' = 'add'; statusHrcompanyModel: 'add' | 'edit' = 'add';
validateEmail: string = 'un-usable'; // '' | 'required' | 'pattern' | 'un-usable' | 'usable'
isEnabledBtnAdmin: boolean = false;
companyId: string; companyId: string;
...@@ -52,6 +56,7 @@ export class MyhrcompanyComponent implements OnInit { ...@@ -52,6 +56,7 @@ export class MyhrcompanyComponent implements OnInit {
} }
constructor( constructor(
private myhrcompanyservice: MyhrcompanyService, private myhrcompanyservice: MyhrcompanyService,
private userService: UserService,
public translate: TranslateService, public translate: TranslateService,
private router: Router private router: Router
) {} ) {}
...@@ -177,8 +182,24 @@ export class MyhrcompanyComponent implements OnInit { ...@@ -177,8 +182,24 @@ export class MyhrcompanyComponent implements OnInit {
this.selectHrcompanyModel.receiptSignature2 = ''; this.selectHrcompanyModel.receiptSignature2 = '';
} }
viewHrcompanyModel(item: HrcompanyModel) {
this.action = 'edit';
this.selectHrcompanyModel = new MyHrcompanyModel(item);
console.log('viewHrcompanyModel is:', this.selectHrcompanyModel);
if (this.selectHrcompanyModel.companyEmail === '') {
this.validateEmail = 'required';
} else {
this.validateEmail = 'usable';
this.onCheckValidateByUsername(this.selectHrcompanyModel.companyEmail || '', 'viewHrcompanyModel');
}
}
saveHrcompanyModel() { saveHrcompanyModel() {
console.log('Before Save, selectHrcompanyModel is:', this.selectHrcompanyModel); console.log(
'Before Save, selectHrcompanyModel is:',
this.selectHrcompanyModel
);
swal({ swal({
title: 'คุณแน่ใจหรือไม่?', title: 'คุณแน่ใจหรือไม่?',
text: 'คุณต้องการบันทึกหรือไม่', text: 'คุณต้องการบันทึกหรือไม่',
...@@ -188,7 +209,8 @@ export class MyhrcompanyComponent implements OnInit { ...@@ -188,7 +209,8 @@ export class MyhrcompanyComponent implements OnInit {
}).then((willSave: any) => { }).then((willSave: any) => {
if (willSave) { if (willSave) {
const action = this.action === 'add' ? 'true' : 'false'; const action = this.action === 'add' ? 'true' : 'false';
this.myhrcompanyservice.postMyHRcompany(this.selectHrcompanyModel,action) this.myhrcompanyservice
.postMyHRcompany(this.selectHrcompanyModel, action)
.subscribe( .subscribe(
(result) => { (result) => {
console.log(result); console.log(result);
...@@ -204,4 +226,131 @@ export class MyhrcompanyComponent implements OnInit { ...@@ -204,4 +226,131 @@ export class MyhrcompanyComponent implements OnInit {
} }
}); });
} }
onCheckCompanyEmail() {
const email = this.selectHrcompanyModel.companyEmail?.trim();
if (!email) {
this.validateEmail = 'required';
return;
}
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailPattern.test(email)) {
this.validateEmail = 'pattern';
//this.selectHrcompanyModel.companyEmail = '';
} else {
this.onCheckValidateByUsername(email, 'onCheckCompanyEmail');
}
}
onCheckValidateByUsername(email: string, fromFun: string) {
if (!email) {
swal('ข้อผิดพลาด', 'ไม่มีข้อมูล อีเมลบริษัท', 'error');
return;
}
this.userService.validateByUsername(email).subscribe({
next: (response: ResponseModel) => {
console.log('validate-user success', response);
if (fromFun === 'viewHrcompanyModel') {
this.isEnabledBtnAdmin = response.success ? true : false; // ปุ่มสร้าง user admin จะ enable ก็ต่อเมื่อยังไม่มี user นี้ในระบบ
}
if (fromFun === 'onCheckCompanyEmail') {
const isSuccess = response.success;
const userDue = response.resultObject as any;
// เช็คไว้ถ้าข้อมูลที่ซ้ำตรงกันกับ companyId ที่เลือกอยู่ จะเป็นข้อมูลของบริษัทตัวเอง usable
const chkUsername = userDue ? userDue.username.split(',')[0] : '';
const chkCompanyId = userDue ? userDue.companyId : '';
const curCompanyId = this.selectHrcompanyModel.companyId || ''
this.validateEmail = isSuccess ? 'usable' : chkUsername === email && chkCompanyId === curCompanyId ? 'usable' : 'un-usable';
this.isEnabledBtnAdmin = isSuccess ? true : false; // ปุ่มสร้าง user admin จะ enable ก็ต่อเมื่อยังไม่มี user นี้ในระบบ
}
},
error: (error) => {
console.error("error can't get data", error);
const errMsg = error?.error?.message || 'ไม่สามารถดึงข้อมูลได้';
swal('เกิดข้อผิดพลาด', errMsg, 'error');
},
});
}
generateCompanySource(companyId: string) {
if (!companyId) {
swal('ข้อผิดพลาด', 'ไม่มีข้อมูล companyId', 'error');
return;
}
this.myhrcompanyservice.generateCompanySource(companyId).subscribe({
next: (response: ResponseModel) => {
console.log('generateCompanySource ', response);
if (response.success === true) {
swal(
'บันทึกสำเร็จ!!',
'อัพเดตข้อมูลแหล่งที่มาบริษัท เรียบร้อย',
'success'
);
this.ngOnInit();
} else {
const errMsg =
response.message || 'ไม่สามารถอัพเดตข้อมูลแหล่งที่มาบริษัท ได้';
swal('เกิดข้อผิดพลาด', errMsg, 'error');
return;
}
},
error: (error) => {
console.error("error can't get data", error);
const errMsg = error?.error?.message || 'ไม่สามารถดึงข้อมูลได้';
swal('เกิดข้อผิดพลาด', errMsg, 'error');
},
});
}
generateUsersAdmin(companyId: string) {
if (!companyId) {
swal('ข้อผิดพลาด', 'ไม่มีข้อมูล companyId', 'error');
return;
}
this.myhrcompanyservice.generateUsersAdmin(companyId).subscribe({
next: (response: ResponseModel) => {
console.log('generateUsersAdmin ', response);
if (response.success === true) {
const userResult = response.resultObject as any;
const msg = "User : " + (userResult ? userResult.username : '') + '\n' +
"Password : " + (userResult ? userResult.password : '') + '\n';
swal('บันทึกสำเร็จ!!', 'สร้าง User Admin เรียบร้อย\n\n '+ msg, 'success');
this.ngOnInit();
} else if (
response.success === false &&
response.message === 'Username is duplicate.'
) {
const errMsg =
'username : ' +
(response.resultObject && (response.resultObject as any).username
? (response.resultObject as any).username
: '');
swal(
'เกิดข้อผิดพลาด',
'อีเมลบริษัทซ้ำกับ user ท่านอื่น \n' + errMsg, // \n ใช้ขึ้นบรรทัดใหม่
'error'
);
return;
} else {
const errMsg = 'ไม่สามารถสร้าง user admin ได้';
swal('เกิดข้อผิดพลาด', errMsg, 'error');
return;
}
},
error: (error) => {
console.error("error can't get data", error);
const errMsg = error?.error?.message || 'ไม่สามารถดึงข้อมูลได้';
swal('เกิดข้อผิดพลาด', errMsg, 'error');
},
});
}
} }
...@@ -4,6 +4,7 @@ import { Observable } from 'rxjs'; ...@@ -4,6 +4,7 @@ import { Observable } from 'rxjs';
import { environment } from '../../../../environments/environment'; import { environment } from '../../../../environments/environment';
import { HrcompanyModel } from '../../models/mylearn/myhrcompany.model'; import { HrcompanyModel } from '../../models/mylearn/myhrcompany.model';
import { AlertModel } from '../../models/alert.model'; import { AlertModel } from '../../models/alert.model';
import { ResponseModel } from '../../models/response.model.';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
...@@ -25,7 +26,18 @@ export class MyhrcompanyService { ...@@ -25,7 +26,18 @@ export class MyhrcompanyService {
return this.http.get<HrcompanyModel[]>(this.urlApi + '/lists'); return this.http.get<HrcompanyModel[]>(this.urlApi + '/lists');
} }
postMyHRcompany(body: HrcompanyModel, action?: string): Observable<AlertModel> { // action = 'true' จะให้ทำ process สร้าง user member -> admin ให้เลย postMyHRcompany(body: HrcompanyModel, action?: string): Observable<AlertModel> {
return this.http.post<AlertModel>(this.urlApi + '?action=' + action, body); return this.http.post<AlertModel>(this.urlApi + '?action=' + action, body);
} }
// update data company-source and user-type
generateCompanySource(companyId: string): Observable<ResponseModel> {
return this.http.get<ResponseModel>(`${this.urlApi}/generate-source?companyId=${companyId}`);
}
// create user admin of company
generateUsersAdmin(companyId: string): Observable<ResponseModel> {
return this.http.get<ResponseModel>(`${this.urlApi}/generate-usersadmin?companyId=${companyId}`
);
}
} }
...@@ -4,50 +4,60 @@ import { Observable } from 'rxjs'; ...@@ -4,50 +4,60 @@ import { Observable } from 'rxjs';
import { environment } from '../../../../environments/environment'; import { environment } from '../../../../environments/environment';
import { ResponseModel, PageResponseModel } from '../../models/base.model'; import { ResponseModel, PageResponseModel } from '../../models/base.model';
import { ProfileModel, MyProfileModel } from '../../models/mylearn/user.model'; import { ProfileModel, MyProfileModel } from '../../models/mylearn/user.model';
import { UserAccountModel, MyUserAccountModel } from '../../models/mylearn/user-account.model'; import {
UserAccountModel,
MyUserAccountModel,
} from '../../models/mylearn/user-account.model';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root',
}) })
export class UserService { export class UserService {
private urlApi = environment.baseUrlMylearn + "/users"; private urlApi = environment.baseUrlMylearn + '/users';
constructor( constructor(private http: HttpClient, private translate: TranslateService) {}
private http: HttpClient,
private translate: TranslateService
) { }
getAll(): Observable<MyProfileModel[]> { getAll(): Observable<MyProfileModel[]> {
return this.http.get<ProfileModel[]>(this.urlApi).pipe( return this.http
map(list => list.map(e => new MyProfileModel(e, this.translate))) .get<ProfileModel[]>(this.urlApi)
.pipe(
map((list) => list.map((e) => new MyProfileModel(e, this.translate)))
); );
} }
getByUsername(username: string): Observable<MyProfileModel> { getByUsername(username: string): Observable<MyProfileModel> {
return this.http.get<ProfileModel>(`${this.urlApi}/${username}`).pipe( return this.http
map(e => new MyProfileModel(e, this.translate)) .get<ProfileModel>(`${this.urlApi}/${username}`)
); .pipe(map((e) => new MyProfileModel(e, this.translate)));
} }
getLists(): Observable<MyProfileModel[]> { getLists(): Observable<MyProfileModel[]> {
return this.http.get<ProfileModel[]>(`${this.urlApi}/lists`).pipe( return this.http
map(list => list.map(e => new MyProfileModel(e, this.translate))) .get<ProfileModel[]>(`${this.urlApi}/lists`)
.pipe(
map((list) => list.map((e) => new MyProfileModel(e, this.translate)))
); );
} }
getListsByCompany(companyId: string): Observable<MyUserAccountModel[]> { getListsByCompany(companyId: string): Observable<MyUserAccountModel[]> {
return this.http return this.http
.get<UserAccountModel[]>(`${this.urlApi}/lists?companyId=${companyId}`) .get<UserAccountModel[]>(`${this.urlApi}/lists?companyId=${companyId}`)
.pipe(map(list => list.map(e => new MyUserAccountModel(e, this.translate)))); .pipe(
map((list) =>
list.map((e) => new MyUserAccountModel(e, this.translate))
)
);
} }
save(user: UserAccountModel): Observable<ResponseModel> { save(user: UserAccountModel): Observable<ResponseModel> {
return this.http.post<ResponseModel>(this.urlApi, new MyUserAccountModel (user)); return this.http.post<ResponseModel>(
this.urlApi,
new MyUserAccountModel(user)
);
} }
delete(user: ProfileModel): Observable<ResponseModel> { delete(user: ProfileModel): Observable<ResponseModel> {
const options = { const options = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }), headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
...@@ -64,10 +74,17 @@ export class UserService { ...@@ -64,10 +74,17 @@ export class UserService {
return this.http.delete<ResponseModel>(this.urlApi, options); return this.http.delete<ResponseModel>(this.urlApi, options);
} }
changePassword(username: string, newPassword: string): Observable<ResponseModel> { changePassword(
username: string,
newPassword: string
): Observable<ResponseModel> {
return this.http.post<ResponseModel>(`${this.urlApi}/change-password`, { return this.http.post<ResponseModel>(`${this.urlApi}/change-password`, {
username, username,
newPassword, newPassword,
}); });
} }
validateByUsername(username: string): Observable<ResponseModel> {
return this.http.get<ResponseModel>(`${this.urlApi}/validate/${username}`);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment