-
-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathevidence-store.ts
More file actions
197 lines (162 loc) · 6.24 KB
/
evidence-store.ts
File metadata and controls
197 lines (162 loc) · 6.24 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { YamlService } from '../service/yaml-loader/yaml-loader.service';
import { ActivityId, EvidenceId } from './types';
export interface EvidenceAttachment {
type: string; // e.g. 'document', 'image', 'link'
externalLink: string; // URL
}
export interface EvidenceEntry {
id: EvidenceId; // stable UUID for this entry
teams: string[];
title: string;
evidenceRecorded: string; // ISO date string
reviewer?: string;
description: string;
attachment?: EvidenceAttachment[];
}
export type EvidenceData = Record<ActivityId, EvidenceEntry[]>;
const LOCALSTORAGE_KEY: string = 'evidence';
export class EvidenceStore {
private yamlService: YamlService = new YamlService();
private _evidence: EvidenceData = {};
// ─── Lifecycle ────────────────────────────────────────────
public initFromLocalStorage(): void {
const stored = this.retrieveStoredEvidence();
if (stored) {
this.addEvidenceData(stored);
}
}
// ─── Accessors ────────────────────────────────────────────
public getEvidenceData(): EvidenceData {
return this._evidence;
}
public getEvidence(activityUuid: ActivityId): EvidenceEntry[] {
return this._evidence[activityUuid] || [];
}
public hasEvidence(activityUuid: ActivityId): boolean {
return (this._evidence[activityUuid]?.length || 0) > 0;
}
public getEvidenceCount(activityUuid: ActivityId): number {
return this._evidence[activityUuid]?.length ?? 0;
}
public getTotalEvidenceCount(): number {
let count = 0;
for (const uuid in this._evidence) {
count += this._evidence[uuid].length;
}
return count;
}
public getActivityUuidsWithEvidence(): ActivityId[] {
return Object.keys(this._evidence).filter(uuid => this._evidence[uuid].length > 0);
}
// ─── Mutators ────────────────────────────────────────────
public addEvidenceData(newEvidence: EvidenceData): void {
if (!newEvidence) return;
for (const activityUuid in newEvidence) {
if (!this._evidence[activityUuid]) {
this._evidence[activityUuid] = [];
}
const newEntries = newEvidence[activityUuid];
if (Array.isArray(newEntries)) {
for (const entry of newEntries) {
const existingIndex = this._evidence[activityUuid].findIndex(e => e.id === entry.id);
if (existingIndex !== -1) {
this._evidence[activityUuid][existingIndex] = entry;
} else {
this._evidence[activityUuid].push(entry);
}
}
}
}
}
public replaceEvidenceData(data: EvidenceData): void {
this._evidence = data;
this.saveToLocalStorage();
}
public addEvidence(activityUuid: ActivityId, entry: EvidenceEntry): void {
if (!this._evidence[activityUuid]) {
this._evidence[activityUuid] = [];
}
this._evidence[activityUuid].push(entry);
this.saveToLocalStorage();
}
public updateEvidence(
activityUuid: ActivityId,
entryId: EvidenceId,
updatedEntry: Partial<EvidenceEntry>
): void {
const entries = this._evidence[activityUuid];
if (!entries) {
console.warn(`No evidence found for activity ${activityUuid}`);
return;
}
const index = entries.findIndex(e => e.id === entryId);
if (index === -1) {
console.warn(`Cannot find evidence with id ${entryId} for activity ${activityUuid}`);
return;
}
// Immutable update for Angular change detection
entries[index] = { ...entries[index], ...updatedEntry };
this.saveToLocalStorage();
}
public deleteEvidence(activityUuid: ActivityId, entryId: EvidenceId): void {
const entries = this._evidence[activityUuid];
if (!entries) {
console.warn(`No evidence found for activity ${activityUuid}`);
return;
}
const index = entries.findIndex(e => e.id === entryId);
if (index === -1) {
console.warn(`Cannot find evidence with id ${entryId} for activity ${activityUuid}`);
return;
}
entries.splice(index, 1);
if (entries.length === 0) {
delete this._evidence[activityUuid];
}
this.saveToLocalStorage();
}
public renameTeam(oldName: string, newName: string): void {
console.log(`Renaming team '${oldName}' to '${newName}' in evidence store`);
for (const uuid in this._evidence) {
this._evidence[uuid].forEach(entry => {
entry.teams = entry.teams.map(t => (t === oldName ? newName : t));
});
}
this.saveToLocalStorage();
}
// ─── Serialization ──────────────────────────────────────
public asYamlString(): string {
return this.yamlService.stringify({ evidence: this._evidence });
}
public saveToLocalStorage(): void {
const yamlStr = this.asYamlString();
localStorage.setItem(LOCALSTORAGE_KEY, yamlStr);
}
public deleteBrowserStoredEvidence(): void {
console.log('Deleting evidence from browser storage');
localStorage.removeItem(LOCALSTORAGE_KEY);
}
public retrieveStoredEvidenceYaml(): string | null {
return localStorage.getItem(LOCALSTORAGE_KEY);
}
public retrieveStoredEvidence(): EvidenceData | null {
const yamlStr = this.retrieveStoredEvidenceYaml();
if (!yamlStr) return null;
const parsed = this.yamlService.parse(yamlStr);
return parsed?.evidence ?? null;
}
// ─── Helpers ─────────────────────────────────────────────
private isDuplicateEntry(activityUuid: ActivityId, entry: EvidenceEntry): boolean {
const existing = this._evidence[activityUuid];
if (!existing) return false;
return existing.some(e => e.id === entry.id);
}
public static todayDateString(): string {
const now = new Date();
return now.toISOString().substring(0, 10);
}
// to be used when creating new evidence entries to ensure they have a stable UUID
public static generateId(): string {
return crypto.randomUUID?.() ?? `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
}
}