289 lines
7.9 KiB
TypeScript
Raw Normal View History

2019-09-18 15:02:21 +09:00
import { Injectable, Inject } from '@angular/core';
import { Router } from '@angular/router';
2019-10-02 14:34:17 +09:00
import { of } from 'rxjs';
import { catchError, exhaustMap, map, tap, switchMap } from 'rxjs/operators';
2019-09-18 15:02:21 +09:00
2019-09-27 12:53:21 +09:00
import { Actions, ofType, createEffect } from '@ngrx/effects';
2019-10-04 10:19:55 +09:00
import {
PiService,
Login2Response,
2019-11-12 18:54:21 +09:00
UserTermsActionResponse,
2019-10-04 10:19:55 +09:00
} from '@ucap-webmessenger/pi';
2019-09-19 17:03:39 +09:00
import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native';
import {
DialogService,
ConfirmDialogComponent,
ConfirmDialogData,
2019-11-12 18:54:21 +09:00
ConfirmDialogResult,
2019-09-19 17:03:39 +09:00
} from '@ucap-webmessenger/ui';
2019-09-18 15:02:21 +09:00
import {
loginRedirect,
2019-09-19 17:03:39 +09:00
logout,
logoutConfirmation,
2019-09-24 09:23:30 +09:00
logoutConfirmationDismiss,
webLogin,
webLoginSuccess,
2019-10-04 10:19:55 +09:00
webLoginFailure,
postLogin,
privacyAgree,
privacyDisagree,
privacyAgreeFailure,
privacyAgreeSuccess,
changePassword,
changePasswordFailure,
2019-11-12 18:54:21 +09:00
changePasswordSuccess,
2019-09-18 15:02:21 +09:00
} from './actions';
2019-10-04 10:19:55 +09:00
import {
LoginInfo,
KEY_LOGIN_INFO,
EnvironmentsInfo,
2019-11-12 18:54:21 +09:00
KEY_ENVIRONMENTS_INFO,
2019-10-04 10:19:55 +09:00
} from '@app/types';
import { AppAuthenticationService } from '@app/services/authentication.service';
2019-09-27 12:53:21 +09:00
import { NGXLogger } from 'ngx-logger';
2019-10-04 10:19:55 +09:00
import { Store } from '@ngrx/store';
import { SessionStorageService } from '@ucap-webmessenger/web-storage';
import {
ServiceProtocolService,
2019-11-12 18:54:21 +09:00
UserPasswordSetResponse,
2019-10-04 10:19:55 +09:00
} from '@ucap-webmessenger/protocol-service';
2019-09-18 15:02:21 +09:00
@Injectable()
export class Effects {
2019-10-02 14:34:17 +09:00
webLogin$ = createEffect(() =>
this.actions$.pipe(
ofType(webLogin),
map(action => action),
exhaustMap((params: { loginInfo: LoginInfo; rememberMe: boolean }) =>
this.piService
.login2({
loginId: params.loginInfo.loginId,
loginPw: params.loginInfo.loginPw,
2019-11-12 18:54:21 +09:00
companyCode: params.loginInfo.companyCode,
2019-10-02 14:34:17 +09:00
})
.pipe(
map((res: Login2Response) => {
if ('success' !== res.status.toLowerCase()) {
return webLoginFailure({ error: 'Failed' });
} else {
return webLoginSuccess({
loginInfo: params.loginInfo,
rememberMe: params.rememberMe,
2019-11-12 18:54:21 +09:00
login2Response: res,
2019-10-02 14:34:17 +09:00
});
}
}),
catchError(error => of(webLoginFailure({ error })))
)
)
)
2019-09-18 15:02:21 +09:00
);
2019-09-24 09:23:30 +09:00
webLoginSuccess$ = createEffect(
2019-09-19 14:15:43 +09:00
() =>
this.actions$.pipe(
2019-09-24 09:23:30 +09:00
ofType(webLoginSuccess),
2019-09-24 18:42:53 +09:00
switchMap(params =>
this.nativeService.checkForUpdates().pipe(
map((update: boolean) => {
if (!update) {
this.appAuthenticationService.login(
params.loginInfo,
params.rememberMe
);
this.router.navigate(['/messenger']);
}
}),
catchError(error => of(error))
)
)
2019-09-19 14:15:43 +09:00
),
{ dispatch: false }
2019-09-18 15:02:21 +09:00
);
loginRedirect$ = createEffect(
() =>
this.actions$.pipe(
2019-09-19 17:03:39 +09:00
ofType(loginRedirect),
2019-09-18 15:02:21 +09:00
tap(authed => {
this.router.navigate(['/account/login']);
})
),
{ dispatch: false }
);
2019-09-19 17:03:39 +09:00
logout$ = createEffect(() =>
this.actions$.pipe(
ofType(logout),
map(action => action),
map(() => {
2019-09-19 18:22:13 +09:00
this.appAuthenticationService.logout();
2019-09-19 17:03:39 +09:00
return loginRedirect();
})
)
);
logoutConfirmation$ = createEffect(() =>
this.actions$.pipe(
ofType(logoutConfirmation),
exhaustMap(async () => {
const result = await this.dialogService.open<
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult
>(ConfirmDialogComponent, {
width: '220px',
data: {
title: 'Logout',
2019-11-12 18:54:21 +09:00
message: 'Logout ?',
},
2019-09-19 17:03:39 +09:00
});
return result.choice;
}),
map(result => (result ? logout() : logoutConfirmationDismiss()))
)
);
2019-10-04 10:19:55 +09:00
postLogin$ = createEffect(
() =>
this.actions$.pipe(
ofType(postLogin),
map(action => action.loginRes),
tap(async loginRes => {
const loginInfo = this.sessionStorageService.get<LoginInfo>(
KEY_LOGIN_INFO
);
const environmentsInfo = this.sessionStorageService.get<
EnvironmentsInfo
>(KEY_ENVIRONMENTS_INFO);
if (!loginRes.privateInformationAgree) {
const privacyTotalUrl = this.piService.privacyTotalUrl({
companyCode: loginInfo.companyCode,
userSeq: loginRes.userSeq,
token: loginRes.tokenString,
deviceType: environmentsInfo.deviceType,
localeCode: loginInfo.localeCode,
2019-11-12 18:54:21 +09:00
textOnly: 'true',
2019-10-04 10:19:55 +09:00
});
const result = await this.dialogService.open<
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult
>(ConfirmDialogComponent, {
width: '100%',
height: '500px',
disableClose: true,
data: {
2019-11-12 18:54:21 +09:00
title: '개인정보 동의',
2019-10-04 10:19:55 +09:00
// html: `<iframe id="ifm_privacy" src="${privacyTotalUrl}" style="width: 100%;height: 300px;" />`
2019-11-12 18:54:21 +09:00
},
2019-10-04 10:19:55 +09:00
});
if (result.choice) {
this.store.dispatch(privacyAgree({ loginRes }));
} else {
this.store.dispatch(privacyDisagree());
return;
}
}
if (!!loginRes.passwordExpired) {
const result = await this.dialogService.open<
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult
>(ConfirmDialogComponent, {
width: '100%',
height: '500px',
disableClose: true,
data: {
title: '패스워드 만료',
2019-11-12 18:54:21 +09:00
message: '',
},
2019-10-04 10:19:55 +09:00
});
if (result.choice) {
} else {
return;
}
}
})
),
{ dispatch: false }
);
privacyAgree$ = createEffect(() =>
this.actions$.pipe(
ofType(privacyAgree),
map(action => {
const loginInfo = this.sessionStorageService.get<LoginInfo>(
KEY_LOGIN_INFO
);
const environmentsInfo = this.sessionStorageService.get<
EnvironmentsInfo
>(KEY_ENVIRONMENTS_INFO);
return {
loginInfo,
environmentsInfo,
2019-11-12 18:54:21 +09:00
loginResponse: action.loginRes,
2019-10-04 10:19:55 +09:00
};
}),
exhaustMap(params =>
this.piService
.userTermsAction({
userSeq: params.loginResponse.userSeq,
token: params.loginResponse.tokenString,
2019-11-12 18:54:21 +09:00
deviceType: params.environmentsInfo.deviceType,
2019-10-04 10:19:55 +09:00
})
.pipe(
map((res: UserTermsActionResponse) => {
if ('00' !== res.responseCode) {
return privacyAgreeFailure({ error: 'Failed' });
} else {
return privacyAgreeSuccess();
}
}),
catchError(error => of(privacyAgreeFailure({ error })))
)
)
)
);
changePassword$ = createEffect(() =>
this.actions$.pipe(
ofType(changePassword),
map(action => action.req),
exhaustMap(req =>
this.serviceProtocolService.userPasswordSet(req).pipe(
map((res: UserPasswordSetResponse) => {
return changePasswordSuccess({
2019-11-12 18:54:21 +09:00
res,
2019-10-04 10:19:55 +09:00
});
}),
catchError(error => of(changePasswordFailure({ error })))
)
)
)
);
2019-09-18 15:02:21 +09:00
constructor(
private actions$: Actions,
2019-09-19 17:03:39 +09:00
private router: Router,
2019-10-04 10:19:55 +09:00
private store: Store<any>,
private sessionStorageService: SessionStorageService,
2019-09-18 15:02:21 +09:00
private piService: PiService,
2019-09-19 18:22:13 +09:00
private appAuthenticationService: AppAuthenticationService,
2019-10-04 10:19:55 +09:00
private serviceProtocolService: ServiceProtocolService,
2019-09-19 17:03:39 +09:00
private dialogService: DialogService,
2019-09-27 12:53:21 +09:00
@Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService,
private logger: NGXLogger
2019-09-18 15:02:21 +09:00
) {}
}