This repository has been archived by the owner on Jan 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
case.resolver.ts
94 lines (79 loc) · 3.44 KB
/
case.resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { CaseView } from '../core/cases/case-view.model';
import { ActivatedRouteSnapshot, ParamMap, Resolve, Router } from '@angular/router';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { CasesService } from '../core/cases/cases.service';
import { Response } from '@angular/http';
import { AlertService } from '../core/alert/alert.service';
import 'rxjs/add/operator/catch';
import { DraftService } from '../core/draft/draft.service';
import { Draft } from '@hmcts/ccd-case-ui-toolkit';
@Injectable()
export class CaseResolver implements Resolve<CaseView> {
public static readonly PARAM_JURISDICTION_ID = 'jid';
public static readonly PARAM_CASE_TYPE_ID = 'ctid';
public static readonly PARAM_CASE_ID = 'cid';
public static readonly CASE_CREATED_MSG = 'The case has been created successfully';
// we need to run the CaseResolver on every child route of 'case/:jid/:ctid/:cid'
// this is achieved with runGuardsAndResolvers: 'always' configuration
// we cache the case view to avoid retrieving it for each child route
public cachedCaseView: CaseView;
constructor(private casesService: CasesService,
private draftService: DraftService,
private router: Router,
private alertService: AlertService) {}
resolve(route: ActivatedRouteSnapshot): Observable<CaseView> {
let {jid, ctid, cid} = this.getParams(route.paramMap);
if (!cid) {
// when redirected to case view after a case created, and the user has no READ access,
// the post returns no id
this.navigateToCaseList();
} else {
return this.isRootCaseViewRoute(route) ? this.getAndCacheCaseView(jid, ctid, cid)
: this.cachedCaseView ? Observable.of(this.cachedCaseView)
: this.getAndCacheCaseView(jid, ctid, cid);
}
}
private getParams(pm: ParamMap) {
const jid = pm.get(CaseResolver.PARAM_JURISDICTION_ID);
const ctid = pm.get(CaseResolver.PARAM_CASE_TYPE_ID);
const cid = pm.get(CaseResolver.PARAM_CASE_ID);
return {jid, ctid, cid};
}
private navigateToCaseList() {
this.router.navigate(['/list/case'])
.then(() => this.alertService.success(CaseResolver.CASE_CREATED_MSG));
}
private isRootCaseViewRoute(route: ActivatedRouteSnapshot) {
// is route case/:jid/:ctid/:cid
return ((!route.firstChild || !route.firstChild.url.length) && !this.isTabViewRoute(route));
}
private isTabViewRoute(route: ActivatedRouteSnapshot) {
// is route case/:jid/:ctid/:cid#fragment
return route.firstChild && route.firstChild.fragment;
}
private getAndCacheCaseView(jid, ctid, cid): Observable<CaseView> {
if (Draft.isDraft(cid)) {
return this.getAndCacheDraft(jid, ctid, cid);
} else {
return this.casesService
.getCaseViewV2(cid)
.do(caseView => this.cachedCaseView = caseView)
.catch((error: Response | any) => this.checkAuthorizationError(error));
}
}
private getAndCacheDraft(jid, ctid, cid): Observable<CaseView> {
return this.draftService
.getDraft(jid, ctid, cid)
.do(caseView => this.cachedCaseView = caseView)
.catch((error: Response | any) => this.checkAuthorizationError(error));
}
private checkAuthorizationError(error: any) {
// TODO Should be logged to remote logging infrastructure
console.error(error);
if (error.status !== 401 && error.status !== 403) {
this.router.navigate(['/error']);
}
return Observable.throw(error);
}
}