Py-detect / src /app /shared /case-store.service.ts
Oviya
casedetailspage-update
6f093ab
raw
history blame
5.4 kB
import { Injectable } from '@angular/core';
export interface PoliceCase {
// Optional metadata used by your UI
caseId?: string;
dateTime?: string;
status?: 'Open' | 'Under Investigation' | 'Closed' | 'Archived';
crime: string;
police: {
name: string;
station: string;
address: string;
pincode: string;
dutyPerson: string;
modeOfCrime: string;
information?: string;
};
accused: {
name: string;
age: string | number;
gender: string;
address: string;
occupation?: string;
};
lastUpdated?: string;
nextAction?: string;
casePriority?: 'High' | 'Medium' | 'Low';
reportedBy?: string;
verifiedBy?: string;
briefDescription?: string;
caseCategory?: string;
}
@Injectable({ providedIn: 'root' })
export class CaseStoreService {
private readonly storageKey = 'py_detect_police_cases';
private cases: PoliceCase[] = [];
constructor() {
this.load();
}
/** Create (newest first) */
addPoliceCase(c: PoliceCase): void {
this.cases.unshift(c);
this.save();
}
/** Read */
getPoliceCases(): PoliceCase[] {
return this.cases;
}
/** Update by array index */
updatePoliceCaseAt(index: number, updated: PoliceCase): void {
if (index >= 0 && index < this.cases.length) {
this.cases[index] = updated;
this.save();
}
}
/** Update by caseId */
updatePoliceCaseById(caseId: string, updated: PoliceCase): void {
const idx = this.cases.findIndex(c => c.caseId === caseId);
if (idx !== -1) {
this.cases[idx] = updated;
this.save();
}
}
/**
* Convenience: map Info page reactive-form value to PoliceCase and store it.
* Call with the whole this.form.value from InfopageComponent.
*/
addFromInfoForm(formValue: any): void {
const crime = (formValue && formValue.crime) || {};
const suspect = (formValue && formValue.suspect) || {};
const notes = (formValue && formValue.notes) || {};
const mapped: PoliceCase = {
caseId: crime.caseId || '',
dateTime: crime.dateTime || '',
status: notes.status || 'Open',
crime: crime.crimeType || 'Unknown',
police: {
name: notes.officerInCharge || '—',
station: '—', // not captured on Info page
address: crime.location || '—',
pincode: '', // not captured on Info page
dutyPerson: notes.officerInCharge || '—',
modeOfCrime: crime.crimeType || '—',
information: notes.initialFindings || ''
},
accused: {
name: suspect.fullName || '—',
age: suspect.age || '—',
gender: suspect.gender || '—',
address: suspect.address || '—',
occupation: suspect.alias || ''
}
};
this.addPoliceCase(mapped);
}
/** Add or update from Info page form */
addOrUpdateFromInfoForm(formValue: any): void {
const crime = (formValue && formValue.crime) || {};
const suspect = (formValue && formValue.suspect) || {};
const notes = (formValue && formValue.notes) || {};
const mapped: PoliceCase = {
caseId: crime.caseId || '',
dateTime: crime.dateTime || '',
status: notes.status || 'Open',
crime: crime.crimeType || 'Unknown',
police: {
name: notes.officerInCharge || '—',
station: '—',
address: crime.location || '—',
pincode: '',
dutyPerson: notes.officerInCharge || '—',
modeOfCrime: crime.crimeType || '—',
information: notes.initialFindings || ''
},
accused: {
name: suspect.fullName || '—',
age: suspect.age || '—',
gender: suspect.gender || '—',
address: suspect.address || '—',
occupation: suspect.alias || ''
},
reportedBy: crime.reportedBy || '',
verifiedBy: notes.verifiedBy || '',
briefDescription: crime.briefDescription || '',
};
const idx = this.cases.findIndex(c => c.caseId === mapped.caseId);
if (idx !== -1) {
this.cases[idx] = mapped;
this.save();
} else {
this.addPoliceCase(mapped);
}
}
/** Persist to localStorage (safe to keep; remove if not needed) */
private save(): void {
try { localStorage.setItem(this.storageKey, JSON.stringify(this.cases)); } catch { }
}
/** Load from localStorage */
private load(): void {
try {
const raw = localStorage.getItem(this.storageKey);
this.cases = raw ? (JSON.parse(raw) as PoliceCase[]) : [];
} catch {
this.cases = [];
}
// Seed a default case if none exist (for development/testing)
if (this.cases.length === 0) {
this.cases = [
{
caseId: 'CASE-001',
dateTime: new Date().toISOString(),
status: 'Open',
crime: 'Theft',
police: {
name: 'Officer John Doe',
station: 'Central Station',
address: '123 Main St, City',
pincode: '123456',
dutyPerson: 'Officer John Doe',
modeOfCrime: 'Burglary',
information: 'Initial investigation started.'
},
accused: {
name: 'Jane Smith',
age: 30,
gender: 'Female',
address: '456 Side Rd, City',
occupation: 'Unemployed'
}
}
];
this.save();
}
}
}