/** * Copyright since 2025 Mifos Initiative * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import { CommonModule } from '@angular/common'; import { Component, OnInit, inject } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { Router, RouterModule } from '@angular/router'; import { ApiManagementService, ApiVendor } from '../api-management.service'; /** Every outside API service the CBS uses: vendor, its work, its APIs (IN / OUT) and whether it is switched on. */ @Component({ selector: 'mifosx-api-vendors', standalone: true, imports: [ CommonModule, FormsModule, RouterModule ], templateUrl: './api-vendors.component.html', styleUrls: ['../../credit-bureau/cic-shared.scss'] }) export class ApiVendorsComponent implements OnInit { private service = inject(ApiManagementService); private router = inject(Router); vendors: ApiVendor[] = []; summary: { callsToday: number; failedToday: number; perVendor: Record } = { callsToday: 0, failedToday: 0, perVendor: {} }; loading = false; error: string | null = null; info: string | null = null; adding = false; saving = false; form: { name: string; work: string; environment: ApiVendor['environment'] } = this.blankForm(); ngOnInit(): void { this.load(); } private blankForm() { return { name: '', work: '', environment: 'SIMULATOR' as ApiVendor['environment'] }; } load(): void { this.loading = true; this.service.vendors().subscribe((v) => { this.vendors = v; this.loading = false; }); this.service.summary().subscribe((s) => (this.summary = s)); } count(v: ApiVendor, direction: 'IN' | 'OUT'): number { return v.apis.filter((a) => a.direction === direction).length; } get totalIn(): number { return this.vendors.reduce((s, v) => s + this.count(v, 'IN'), 0); } get totalOut(): number { return this.vendors.reduce((s, v) => s + this.count(v, 'OUT'), 0); } get activeVendors(): number { return this.vendors.filter((v) => v.active).length; } calls(v: ApiVendor): { calls: number; failed: number } { return this.summary.perVendor[v.id] ?? { calls: 0, failed: 0 }; } toggle(v: ApiVendor, event: Event): void { const want = (event.target as HTMLInputElement).checked; (event.target as HTMLInputElement).checked = v.active; this.error = null; this.info = null; this.service.saveVendor(v.id, { active: want }).subscribe({ next: (u) => { v.active = u.active; this.info = `${u.name} is now ${u.active ? 'active' : 'switched off'}.`; }, error: (e) => (this.error = e?.error?.defaultUserMessage ?? 'Could not change the vendor.') }); } startAdd(): void { this.adding = true; this.form = this.blankForm(); this.error = null; } saveNew(): void { this.saving = true; this.error = null; this.service.addVendor(this.form).subscribe({ next: (v) => { this.saving = false; this.adding = false; this.router.navigate([ '/api-management/vendors', v.id ]); }, error: (e) => { this.saving = false; this.error = e?.error?.defaultUserMessage ?? 'Could not add the vendor.'; } }); } }