274 lines
7.6 KiB
TypeScript
Raw Normal View History

2020-03-27 17:39:01 +09:00
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 { SyncProtocolService } from '@ucap/ng-protocol-sync';
import { BuddyProtocolService } from '@ucap/ng-protocol-buddy';
import { DepartmentSelector } from '@ucap/ng-store-organization';
2020-04-03 10:43:34 +09:00
import { LoginActions } from '@ucap/ng-store-authentication';
2020-03-27 17:39:01 +09:00
import * as groupActions from '../group/actions';
import {
buddy2,
buddy2Success,
buddy2Failure,
add,
addFailure,
del,
delFailure,
delSuccess,
update,
updateSuccess,
updateFailure,
delAndClear
} from './actions';
import { BuddySelector, GroupSelector } from '../state';
import { ModuleConfig } from '../../config/module-config';
import { _MODULE_CONFIG } from '../../config/token';
@Injectable()
export class Effects {
2020-04-03 10:43:34 +09:00
postLoginSuccessForBuddy2$ = createEffect(() => {
return this.actions$.pipe(
ofType(LoginActions.loginSuccess),
2020-04-03 11:03:54 +09:00
map(action => buddy2())
2020-04-03 10:43:34 +09:00
);
});
2020-03-27 17:39:01 +09:00
buddy2$ = createEffect(() => {
return this.actions$.pipe(
ofType(buddy2),
2020-04-03 10:43:34 +09:00
withLatestFrom(this.store.pipe(select(BuddySelector.buddySyncDate))),
switchMap(([action, syncDate]) => {
return this.syncProtocolService.buddy2({ syncDate }).pipe(
2020-03-27 17:39:01 +09:00
map(res =>
buddy2Success({
buddyList: res.buddyInfos,
syncDate: res.res.syncDate
})
),
catchError(error => of(buddy2Failure({ error })))
);
})
);
});
add$ = createEffect(() =>
this.actions$.pipe(
ofType(add),
map(action => action.req),
2020-04-03 10:43:34 +09:00
exhaustMap(req =>
2020-03-27 17:39:01 +09:00
this.buddyProtocolService.add(req).pipe(
map((res: BuddyAddResponse) => {
2020-04-03 10:43:34 +09:00
return buddy2();
2020-03-27 17:39:01 +09:00
}),
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(String(req.seq)) > -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 !== String(req.seq)
)
}
})
);
}
}
}
// 소속부서(내부서) 고정그룹 사용시 소속부서원을 삭제하지 않는다.
if (
!!this.moduleConfig.useMyDeptGroup &&
this.moduleConfig.useMyDeptGroup &&
myDeptUserList.filter(deptUser => deptUser.seq === String(req.seq))
.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 })))
)
)
)
);
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 => String(b.seq) === 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 => String(b.seq) === 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<any>,
@Inject(_MODULE_CONFIG) private moduleConfig: ModuleConfig,
private syncProtocolService: SyncProtocolService,
private buddyProtocolService: BuddyProtocolService
) {}
}