-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.js
180 lines (157 loc) · 4.08 KB
/
report.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// @ts-check
import { TYPE_SIGNATURE } from "../form/form";
import { projectFactory } from "../project/project";
import sectionFactory from "./section";
/**
* Report object.
* @typedef {Object} report
* @property {function():string} getId
* @property {function():boolean} isReadonly
* @property {function():Object | undefined} getProject
* @property {function(import("../project/project").project):void} setProject
* @property {function():string} getName
* @property {function(string):void} setName
* @property {function():string} getState
* @property {function(string):void} setState
* @property {function():import("./section").section[]} getSections
* @property {function(number):import("./section").section} getSection
* @property {function} toJSON
*/
/**
* Factory function for creating Report object.
* @param {Object} data initializes report.
* @param {string | undefined} data._id - identity for persistence.
* @param {Object | undefined} data.project
* @param {string} data.name
* @param {string | undefined} data.state
* @param {Object[]} data.sections
* @returns {report} Report object
* @throws {TypeError} data is undefined.
* @throws {TypeError} data.name is undefined, not a string or zero length.
* @throws {TypeError} data.sections is undefined, not an array or zero length.
*/
export default function (data) {
validate(data);
let { _id } = data;
let { name } = data;
let { state } = data;
let project = data.project ? projectFactory(data.project) : undefined;
/**
* @type {import("./section.js").section[]} sections
*/
const sections = data.sections.map((elem) => sectionFactory(elem));
/**
* If the report is not saved id is undefined.
* @returns {string | undefined} report id
*/
function getId() {
return _id;
}
/**
* Report is in readonly state if it contains section that is signature
* type, section's style is readonly and it contains signature image.
* @returns boolean
*/
function isReadonly() {
const sig = sections.filter(
(section) => section.getType() === TYPE_SIGNATURE
);
const elem = sig.find((section) => {
const style = section.getStyle();
const length = section.getImages().length;
return style === "readonly" && length > 0;
});
return elem !== undefined;
}
/**
* @returns {project | undefined}
*/
function getProject() {
return project;
}
/**
* @param {project} param
*/
function setProject(param) {
project = param;
}
/**
* @returns {string}
*/
function getName() {
return name;
}
/**
* @param {string} param
*/
function setName(param) {
name = param;
}
/**
* @returns {string | undefined}
*/
function getState() {
return state;
}
/**
* @param {string} param
*/
function setState(param) {
state = param;
}
/**
* @returns { import("./section.js").section[] } array of report sections
*/
function getSections() {
return sections;
}
/**
* @param {number} id - section id
* @returns {import("./section.js").section | undefined} report section - if no section is found returns undefined.
*/
function getSection(id) {
return sections.find((elem) => elem.getId() === id);
}
function toJSON() {
return {
_id,
project,
name,
state,
sections: sections.map((elem) => elem.toJSON()),
};
}
return Object.freeze({
getId,
isReadonly,
getProject,
setProject,
getName,
setName,
getState,
setState,
getSections,
getSection,
toJSON,
});
}
function validate(data) {
if (!data) {
throw new TypeError("Constructor parameter is undefined.");
}
if (!data.name) {
throw new TypeError("Name is undefined.");
}
if (typeof data.name !== "string") {
throw new TypeError("Name is not a string.");
}
if (!data.sections) {
throw new TypeError("Sections is undefined.");
}
if (!Array.isArray(data.sections)) {
throw new TypeError("Sections is not an array.");
}
if (data.sections.length === 0) {
throw new TypeError("Sections is zero length.");
}
}