Removed settings page mock data and replaced with api calls

This commit is contained in:
2026-09-27 10:58:08 +02:00
parent 6ab01830f3
commit fb27cc8c92
10 changed files with 1067 additions and 557 deletions
+30 -289
View File
@@ -1461,169 +1461,41 @@ export const getApplicantInvites = async (applicantUserId: string): Promise<Invi
return res.data || [];
};
const NOTIFICATIONS_STORAGE_KEY = 'pfm_notifications_cache_v1';
const INITIAL_NOTIFICATIONS: Notification[] = [
{
id: 1001,
tenantId: 1,
organisationId: 101,
recipient: '550e8400-e29b-41d4-a716-446655440000',
platform: NotificationPlatform.Email,
status: NotificationStatus.Delivered,
subject: 'Grant Award Tranche Dispatched - AW-2026-001',
message: 'Statutory notice: Tranche 1 of ZAR 4,200,000.00 has been approved and scheduled for settlement.',
destination: 'cfo@soweto-clinics.org.za',
createdAt: new Date(Date.now() - 1000 * 60 * 45).toISOString(),
sentAt: new Date(Date.now() - 1000 * 60 * 44).toISOString(),
updatedAt: new Date(Date.now() - 1000 * 60 * 44).toISOString(),
error: null,
},
{
id: 1002,
tenantId: 1,
organisationId: 101,
recipient: '772b1100-a29b-41d4-b716-112233445566',
platform: NotificationPlatform.SMS,
status: NotificationStatus.Sent,
subject: 'OTP Security Verification',
message: 'PFM Portal Security: One-Time Passcode for disbursement release approval is 924-811. Expires in 10 mins.',
destination: '+27 82 555 0192',
createdAt: new Date(Date.now() - 1000 * 60 * 120).toISOString(),
sentAt: new Date(Date.now() - 1000 * 60 * 119).toISOString(),
updatedAt: new Date(Date.now() - 1000 * 60 * 119).toISOString(),
error: null,
},
{
id: 1003,
tenantId: 1,
organisationId: 102,
recipient: '331c9900-f12b-41d4-c916-998877665544',
platform: NotificationPlatform.InApp,
status: NotificationStatus.Read,
subject: 'Site Inspection Report Required',
message: 'Milestone 2 physical verification evidence pending upload before next tranche release.',
destination: 'inapp:compliance_officer_102',
createdAt: new Date(Date.now() - 1000 * 60 * 360).toISOString(),
sentAt: new Date(Date.now() - 1000 * 60 * 359).toISOString(),
updatedAt: new Date(Date.now() - 1000 * 60 * 300).toISOString(),
error: null,
},
{
id: 1004,
tenantId: 1,
organisationId: 101,
recipient: '884d2200-e34b-41d4-d816-334455667788',
platform: NotificationPlatform.Webhook,
status: NotificationStatus.Delivered,
subject: 'Disbursement Webhook Trigger',
message: '{"event":"disbursement.approved","disbursementId":402,"amount":1250000.00,"currency":"ZAR"}',
destination: 'https://api.treasury.gov.za/webhooks/pfm-settlement',
createdAt: new Date(Date.now() - 1000 * 60 * 600).toISOString(),
sentAt: new Date(Date.now() - 1000 * 60 * 598).toISOString(),
updatedAt: new Date(Date.now() - 1000 * 60 * 598).toISOString(),
error: null,
},
{
id: 1005,
tenantId: 1,
organisationId: 102,
recipient: '661e5500-a98b-41d4-e116-223344556677',
platform: NotificationPlatform.Email,
status: NotificationStatus.Queued,
subject: 'Quarterly Grant Compliance Audit Notice',
message: 'Scheduled audit review commencing next Monday. Please review the documentation checklist.',
destination: 'director@gauteng-water.gov.za',
createdAt: new Date(Date.now() - 1000 * 60 * 25).toISOString(),
sentAt: null,
updatedAt: new Date(Date.now() - 1000 * 60 * 25).toISOString(),
error: null,
},
{
id: 1006,
tenantId: 1,
organisationId: 101,
recipient: '990f6600-b87b-41d4-f216-112233445500',
platform: NotificationPlatform.WhatsApp,
status: NotificationStatus.Delivered,
subject: 'Contract Execution Confirmation',
message: 'Contract CTR-2026-901 has been countersigned by the Accounting Officer. View in M&E portal.',
destination: '+27 83 444 8920',
createdAt: new Date(Date.now() - 1000 * 60 * 900).toISOString(),
sentAt: new Date(Date.now() - 1000 * 60 * 899).toISOString(),
updatedAt: new Date(Date.now() - 1000 * 60 * 899).toISOString(),
error: null,
},
{
id: 1007,
tenantId: 1,
organisationId: 102,
recipient: '112a3300-c76b-41d4-a316-001122334455',
platform: NotificationPlatform.Email,
status: NotificationStatus.Failed,
subject: 'Budget Line Reallocation Alert',
message: 'Automated warning: Budget line #102-04 variance threshold exceeded 15%.',
destination: 'invalid-address@nonexistent-domain.gov.za',
createdAt: new Date(Date.now() - 1000 * 60 * 1440).toISOString(),
sentAt: new Date(Date.now() - 1000 * 60 * 1438).toISOString(),
updatedAt: new Date(Date.now() - 1000 * 60 * 1438).toISOString(),
error: 'SMTP 550: Mailbox unavailable or domain could not be resolved',
},
];
export const getStoredNotifications = (): Notification[] => {
try {
const raw = localStorage.getItem(NOTIFICATIONS_STORAGE_KEY);
if (!raw) {
localStorage.setItem(NOTIFICATIONS_STORAGE_KEY, JSON.stringify(INITIAL_NOTIFICATIONS));
return INITIAL_NOTIFICATIONS;
}
const parsed = JSON.parse(raw);
return Array.isArray(parsed) && parsed.length > 0 ? parsed : INITIAL_NOTIFICATIONS;
} catch {
return INITIAL_NOTIFICATIONS;
}
};
export const getNotifications = async (organisationId?: string | number): Promise<Notification[]> => {
const targetOrgId =
organisationId !== undefined && organisationId !== '' && organisationId !== null && organisationId !== 'all'
? organisationId
: getCurrentUserTenantId();
const targetOrgId = organisationId !== undefined && organisationId !== '' && organisationId !== null && organisationId !== 'all'
? organisationId
: getCurrentUserTenantId();
if (!targetOrgId || targetOrgId === 'all') {
return getStoredNotifications();
return [];
}
const hashedOrgId = await toolsService.resolveOrganisationId(targetOrgId);
try {
const res = await apiClient.get<Notification[]>(
`/v1/api/communications/norifications/${hashedOrgId}`
`/v1/api/communications/norifications/${encodeURIComponent(hashedOrgId)}`
);
if (res.data && Array.isArray(res.data) && res.data.length > 0) {
if (res.data && Array.isArray(res.data)) {
return res.data;
}
} catch (err: any) {
if (err?.response?.status === 404) {
try {
const res = await apiClient.get<Notification[]>(
`/v1/api/communications/notifications/${hashedOrgId}`
const fallbackRes = await apiClient.get<Notification[]>(
`/v1/api/communications/notifications/${encodeURIComponent(hashedOrgId)}`
);
if (res.data && Array.isArray(res.data) && res.data.length > 0) {
return res.data;
if (fallbackRes.data && Array.isArray(fallbackRes.data)) {
return fallbackRes.data;
}
} catch {
// Fallback to local cache
} catch (fallbackErr) {
console.warn('[getNotifications] Notification fallback route error:', fallbackErr);
}
} else {
console.warn('[getNotifications] API error fetching notifications:', err);
}
}
// Filter stored notifications for this organization or return matching items
const allStored = getStoredNotifications();
const orgFiltered = allStored.filter(
(n) => String(n.organisationId) === String(targetOrgId) || String(n.organisationId) === String(toIntegerId(targetOrgId))
);
return orgFiltered.length > 0 ? orgFiltered : allStored;
return [];
};
// ==========================================
@@ -2082,160 +1954,29 @@ export const updateRule = async (payload: UpdateRuleRequest): Promise<void> => {
// 12. Audit Logs & System Telemetry APIs
// ==========================================
const AUDIT_STORAGE_KEY = 'pfm_audit_ledger_v1';
const INITIAL_AUDIT_LOGS: AuditLog[] = [
{
id: 'AUD-88391',
action: 'USER_AUTHENTICATED',
entityType: 'User',
entityId: 'USR-KHWEZI',
userEmail: 'khwezim@gmail.com',
details: 'User authenticated via OIDC Provider. Multi-tenant security context active.',
createdAt: new Date(Date.now() - 1000 * 60 * 12).toISOString(),
},
{
id: 'AUD-88390',
action: 'DISBURSEMENT_APPROVED',
entityType: 'Disbursement',
entityId: 'DSB-101',
userEmail: 'accounting.officer@treasury.gov.za',
details: 'Authorized tranche release of ZAR 2,500,000.00 for Project #401. EFT reference EFT-2026-9031.',
createdAt: new Date(Date.now() - 1000 * 60 * 48).toISOString(),
},
{
id: 'AUD-88389',
action: 'DISBURSEMENT_SCHEDULED',
entityType: 'Disbursement',
entityId: 'DSB-101',
userEmail: 'grants.officer@dst.gov.za',
details: 'Scheduled tranche of ZAR 2,500,000.00 linked to Milestone Deliverable #201.',
createdAt: new Date(Date.now() - 1000 * 60 * 95).toISOString(),
},
{
id: 'AUD-88388',
action: 'BUDGET_LINE_CREATED',
entityType: 'Budget',
entityId: 'BDG-401-01',
userEmail: 'khwezim@gmail.com',
details: 'Established Specialized Equipment & Prototype Probes budget category for ZAR 4,500,000.00 against Project #401.',
createdAt: new Date(Date.now() - 1000 * 60 * 180).toISOString(),
},
{
id: 'AUD-88387',
action: 'AWARD_REGISTERED',
entityType: 'Award',
entityId: 'AWD-2026-441',
userEmail: 'khwezim@gmail.com',
details: 'Registered approved statutory funding award for ZAR 10,000,000.00 (Programme #301, Org #101).',
createdAt: new Date(Date.now() - 1000 * 60 * 320).toISOString(),
},
{
id: 'AUD-88386',
action: 'RISK_EVALUATED',
entityType: 'Risk',
entityId: 'RSK-202',
userEmail: 'risk.compliance@treasury.gov.za',
details: 'Evaluated ISO 31000 Risk: Supply Chain Logistics & Component Delay. Residual risk score set to Medium (9).',
createdAt: new Date(Date.now() - 1000 * 60 * 480).toISOString(),
},
{
id: 'AUD-88385',
action: 'EVIDENCE_VERIFIED',
entityType: 'Evidence',
entityId: 'EVD-993',
userEmail: 'evaluator@nrf.ac.za',
details: 'Verified milestone completion evidence report with cryptographic hash verification.',
createdAt: new Date(Date.now() - 1000 * 60 * 720).toISOString(),
},
{
id: 'AUD-88384',
action: 'INVITATION_DISPATCHED',
entityType: 'Invite',
entityId: 'INV-1092',
userEmail: 'khwezim@gmail.com',
details: 'Dispatched statutory onboarding invitation to reviewer@institution.ac.za via Email SMTP gateway.',
createdAt: new Date(Date.now() - 1000 * 60 * 1050).toISOString(),
},
{
id: 'AUD-88383',
action: 'DIRECTORY_SYNCED',
entityType: 'Security',
entityId: 'TENANT-001',
userEmail: 'system@treasury.gov.za',
details: 'Executed backend synchronization handshake. 12 active users and authorization claims validated.',
createdAt: new Date(Date.now() - 1000 * 60 * 1440).toISOString(),
},
];
export const getStoredAuditLogs = (): AuditLog[] => {
export const getAuditLogs = async (page = 1, pageSize = 50): Promise<AuditLog[]> => {
try {
const raw = localStorage.getItem(AUDIT_STORAGE_KEY);
if (!raw) {
localStorage.setItem(AUDIT_STORAGE_KEY, JSON.stringify(INITIAL_AUDIT_LOGS));
return INITIAL_AUDIT_LOGS;
const res = await apiClient.post<AuditLog[]>('/v1/api/governance/auditlog', {
index: (page - 1) * pageSize,
page,
pageSize,
});
if (res.data && Array.isArray(res.data)) {
return res.data;
}
const parsed = JSON.parse(raw);
return Array.isArray(parsed) && parsed.length > 0 ? parsed : INITIAL_AUDIT_LOGS;
} catch {
return INITIAL_AUDIT_LOGS;
}
};
export const recordAuditLog = (
log: Omit<AuditLog, 'id' | 'createdAt'> & { id?: string; createdAt?: string }
): AuditLog => {
const current = getStoredAuditLogs();
const newEntry: AuditLog = {
id: log.id || `AUD-${Math.floor(10000 + Math.random() * 90000)}`,
action: log.action,
entityType: log.entityType,
entityId: log.entityId,
userEmail: log.userEmail || 'khwezim@gmail.com',
details: log.details,
createdAt: log.createdAt || new Date().toISOString(),
};
const updated = [newEntry, ...current];
try {
localStorage.setItem(AUDIT_STORAGE_KEY, JSON.stringify(updated));
return [];
} catch (err) {
console.warn('Failed to save audit log to localStorage:', err);
console.error('[getAuditLogs] API request failed:', err);
return [];
}
return newEntry;
};
export const getAuditLogs = async (page = 1, pageSize = 100): Promise<AuditLog[]> => {
// Try candidate backend endpoints first
const endpoints = [
`/v1/api/governance/audit/${page}/${pageSize}`,
`/v1/api/governance/audit`,
`/v1/api/audit/${page}/${pageSize}`,
`/v1/api/audit`,
];
for (const ep of endpoints) {
try {
const res = await apiClient.get<AuditLog[]>(ep);
if (res.data && Array.isArray(res.data) && res.data.length > 0) {
return res.data;
}
} catch {
// fallback to next candidate or stored ledger
}
}
return getStoredAuditLogs();
};
export const getAuditLog = async (auditId: string): Promise<AuditLog | null> => {
export const getAuditLog = async (auditId: string | number): Promise<AuditLog | null> => {
try {
const res = await apiClient.get<AuditLog>(`/v1/api/governance/audit/${auditId}`);
if (res.data) return res.data;
const logs = await getAuditLogs(1, 100);
return logs.find((l) => String(l.id) === String(auditId)) || null;
} catch {
// fallback
return null;
}
const logs = getStoredAuditLogs();
return logs.find((l) => String(l.id) === String(auditId)) || null;
};