import { of } from 'rxjs'; import { catchError, exhaustMap, map, withLatestFrom, switchMap, tap } from 'rxjs/operators'; import { Injectable, Inject } from '@angular/core'; import { Store, select } from '@ngrx/store'; import { Actions, ofType, createEffect } from '@ngrx/effects'; import { AddResponse as BuddyAddResponse, DelResponse as BuddyDelResponse, UpdateResponse as BuddyUpdateResponse } from '@ucap/protocol-buddy'; import { UserNicknameRequest as NicknameRequest, UserNicknameResponse as NicknameResponse } from '@ucap/protocol-info'; import { SyncProtocolService } from '@ucap/ng-protocol-sync'; import { BuddyProtocolService } from '@ucap/ng-protocol-buddy'; import { DepartmentSelector, PresenceActions } from '@ucap/ng-store-organization'; import { LoginActions } from '@ucap/ng-store-authentication'; import * as groupActions from '../group/actions'; import { buddy2, buddy2Success, buddy2Failure, add, addFailure, del, delFailure, delSuccess, update, updateSuccess, updateFailure, nickname, nicknameSuccess, nicknameFailure, delAndClear } from './actions'; import { BuddySelector, GroupSelector } from '../state'; import { ModuleConfig } from '../../config/module-config'; import { _MODULE_CONFIG } from '../../config/token'; import { InfoProtocolService } from '@ucap/ng-protocol-info'; @Injectable() export class Effects { sessionCreatedForBuddy2$ = createEffect(() => { return this.actions$.pipe( ofType(LoginActions.sessionCreated), map((action) => buddy2()) ); }); buddy2$ = createEffect(() => { return this.actions$.pipe( ofType(buddy2), withLatestFrom(this.store.pipe(select(BuddySelector.buddySyncDate))), switchMap(([action, syncDate]) => { return this.syncProtocolService.buddy2({ syncDate }).pipe( map((res) => { // Buddy Presence this.store.dispatch( PresenceActions.bulkInfo({ divCd: 'buddyBulk', userSeqs: res.buddyInfos.map((userInfo) => userInfo.seq + '') }) ); return buddy2Success({ buddyList: res.buddyInfos, syncDate: res.res.syncDate }); }), catchError((error) => of(buddy2Failure({ error }))) ); }) ); }); add$ = createEffect(() => this.actions$.pipe( ofType(add), map((action) => action.req), exhaustMap((req) => this.buddyProtocolService.add(req).pipe( map((res: BuddyAddResponse) => { return buddy2(); }), catchError((error) => of(addFailure({ error }))) ) ) ) ); del$ = createEffect(() => this.actions$.pipe( ofType(del), map((action) => action.req), exhaustMap((req) => this.buddyProtocolService.del(req).pipe( map((res: BuddyDelResponse) => { return delSuccess({ res }); }), catchError((error) => of(delFailure({ error }))) ) ) ) ); delAndClear$ = createEffect( () => { return this.actions$.pipe( ofType(delAndClear), withLatestFrom( this.store.pipe(select(GroupSelector.groups)), this.store.pipe(select(DepartmentSelector.myDepartmentUserInfoList)) // 내 부서원 비교. ), tap(([req, groupList, myDeptUserList]) => { for (const group of groupList) { if (group.userSeqs.indexOf(req.seq as any) > -1) { // 소속부서(내부서) 고정그룹 사용시 소속부서원을 삭제하지 않는다. if ( !!this.moduleConfig.useMyDeptGroup && this.moduleConfig.useMyDeptGroup && !!this.moduleConfig.fixedGroupSeqs && this.moduleConfig.fixedGroupSeqs.filter( (fixedGroupSeq) => fixedGroupSeq === group.seq ).length > 0 ) { // skip;; } else { // Group Clear.. this.store.dispatch( groupActions.update({ req: { groupSeq: group.seq, groupName: group.name, userSeqs: group.userSeqs.filter( (userSeq) => userSeq !== (req.seq as any) ) } }) ); } } } // 소속부서(내부서) 고정그룹 사용시 소속부서원을 삭제하지 않는다. if ( !!this.moduleConfig.useMyDeptGroup && this.moduleConfig.useMyDeptGroup && !!myDeptUserList && myDeptUserList.filter( (deptUser) => deptUser.seq === (req.seq as any) ).length > 0 ) { // skip;; } else { // Favorit Clear.. this.store.dispatch( update({ req: { seq: req.seq, isFavorit: false } }) ); // Buddy Clear.. this.store.dispatch(del({ req: { userSeqs: [String(req.seq)] } })); } }) ); }, { dispatch: false } ); update$ = createEffect(() => this.actions$.pipe( ofType(update), map((action) => action.req), exhaustMap((req) => this.buddyProtocolService.update(req).pipe( map((res: BuddyUpdateResponse) => { return updateSuccess({ res }); }), catchError((error) => of(updateFailure({ error }))) ) ) ) ); nickname$ = createEffect(() => this.actions$.pipe( ofType(nickname), map((action) => action.req), exhaustMap((req) => this.infoProtocolService.userNickname(req).pipe( map((res: NicknameResponse) => { return nicknameSuccess({ res }); }), catchError((error) => of(nicknameFailure({ error }))) ) ) ) ); groupCreateSuccess$ = createEffect( () => { return this.actions$.pipe( ofType(groupActions.createSuccess), withLatestFrom(this.store.pipe(select(BuddySelector.buddies))), tap(([action, buddyList]) => { const targetUserSeqs = action.targetUserSeqs; if (!!targetUserSeqs && 0 < targetUserSeqs.length) { const addBuddyList: string[] = []; targetUserSeqs.forEach((userSeq) => { if (!buddyList) { addBuddyList.push(userSeq); return; } const index = buddyList.findIndex( (b) => b.seq === Number(userSeq) ); if (-1 < index) { addBuddyList.push(userSeq); return; } }); if (addBuddyList.length > 0) { this.store.dispatch(add({ req: { userSeqs: addBuddyList } })); } } }) ); }, { dispatch: false } ); groupUpdateMemberSuccess$ = createEffect( () => { return this.actions$.pipe( ofType(groupActions.updateMemberSuccess), withLatestFrom(this.store.pipe(select(BuddySelector.buddies))), tap(([action, buddyList]) => { const targetUserSeqs = action.targetUserSeqs; const userSeqsForDelete = action.userSeqsForDelete; if (!!targetUserSeqs && 0 < targetUserSeqs.length) { const addBuddyList: string[] = []; targetUserSeqs.forEach((userSeq) => { if (!buddyList) { addBuddyList.push(userSeq); return; } const index = buddyList.findIndex( (b) => b.seq === Number(userSeq) ); if (-1 < index) { addBuddyList.push(userSeq); return; } }); if (addBuddyList.length > 0) { this.store.dispatch(add({ req: { userSeqs: addBuddyList } })); } } if (!!userSeqsForDelete && 0 < userSeqsForDelete.length) { userSeqsForDelete.forEach((userSeq) => { this.store.dispatch( update({ req: { seq: Number(userSeq), isFavorit: false } }) ); }); this.store.dispatch(del({ req: { userSeqs: userSeqsForDelete } })); } }) ); }, { dispatch: false } ); constructor( private actions$: Actions, private store: Store, @Inject(_MODULE_CONFIG) private moduleConfig: ModuleConfig, private syncProtocolService: SyncProtocolService, private buddyProtocolService: BuddyProtocolService, private infoProtocolService: InfoProtocolService ) {} }