@idsoftsource/initial-process 4.2.6 → 4.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,7 +8,7 @@ import * as i2 from '@angular/router';
8
8
  import { RouterModule, Router } from '@angular/router';
9
9
  import * as i1$1 from 'ngx-bootstrap/modal';
10
10
  import { ModalModule } from 'ngx-bootstrap/modal';
11
- import { catchError, throwError, shareReplay, map, Subject, takeUntil, of, firstValueFrom, tap as tap$1, Subscription, switchMap, finalize, EMPTY } from 'rxjs';
11
+ import { catchError, throwError, shareReplay, map, Subject, takeUntil, of, firstValueFrom, tap as tap$1, Subscription, switchMap, concat, toArray, finalize } from 'rxjs';
12
12
  import * as i1 from '@angular/common/http';
13
13
  import { HttpClient, HttpParams, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
14
14
  import * as i9 from '@auth0/auth0-angular';
@@ -35353,6 +35353,9 @@ class UserCoverageAreaService {
35353
35353
  bulkDelete(ids) {
35354
35354
  return this.httpClient.post(`${this.baseUrl}/BulkDelete`, ids);
35355
35355
  }
35356
+ bulkUpdate(models) {
35357
+ return this.httpClient.post(`${this.baseUrl}/BulkUpdate`, models);
35358
+ }
35356
35359
  bulkInsert(models) {
35357
35360
  return this.httpClient.post(`${this.baseUrl}/CreateUserCoverageArea`, models);
35358
35361
  }
@@ -35587,7 +35590,9 @@ class Step2CoverageComponent {
35587
35590
  userId;
35588
35591
  emailId;
35589
35592
  userDetail;
35590
- existingCoverageIds = [];
35593
+ // Saved coverage record per state, used to decide create / update / delete on save.
35594
+ // The full record is kept because BulkUpdate checks its audit fields for concurrency.
35595
+ existingByState = new Map();
35591
35596
  initialCoverageSnapshot;
35592
35597
  constructor(store, usMapService, userCoverageAreaService, alertService, roleContextService, tokenService) {
35593
35598
  this.store = store;
@@ -35596,7 +35601,6 @@ class Step2CoverageComponent {
35596
35601
  this.alertService = alertService;
35597
35602
  this.roleContextService = roleContextService;
35598
35603
  this.tokenService = tokenService;
35599
- this.usMapService.getUsMapCoordinates().then((x) => this.usCoordinates = x);
35600
35604
  }
35601
35605
  // This step only ever runs inside the initial-setup wizard, so the role picked
35602
35606
  // in role-select (surfaced here as roleData, or mirrored onto the shared
@@ -35611,6 +35615,8 @@ class Step2CoverageComponent {
35611
35615
  this.userId = this.roleContextService?.tempUserContext()?.userId ?? await this.tokenService?.getUserId();
35612
35616
  this.emailId = this.roleContextService?.tempUserContext()?.email ?? await this.tokenService?.getUserEmail();
35613
35617
  this.filteredLocations = [{ label: 'Current Coverage Area', locations: [] }];
35618
+ // Prefill needs the full county list, so load coordinates before coverage
35619
+ this.usCoordinates = await this.usMapService.getUsMapCoordinates();
35614
35620
  this.getCoverage(this.userId);
35615
35621
  }
35616
35622
  toggleMap() {
@@ -35634,24 +35640,27 @@ class Step2CoverageComponent {
35634
35640
  newCoverages.push({
35635
35641
  state,
35636
35642
  isShow: true,
35637
- counties: this.usCoordinates.locations
35638
- .filter((l) => l.state?.trim() === state && l.county)
35639
- .sort((a, b) => a.county.localeCompare(b.county))
35640
- .map((l) => ({
35641
- stateCode: state,
35642
- countyName: l.county,
35643
- latitude: l.latitude,
35644
- longitude: l.longitude,
35645
- fillColor: '#E6E6E6',
35646
- strokeColor: '#fff',
35647
- checked: false
35648
- }))
35643
+ counties: this.buildStateCounties(state)
35649
35644
  });
35650
35645
  }
35651
35646
  }
35652
35647
  this.coverages = newCoverages;
35653
35648
  this.updateFilteredLocations();
35654
35649
  }
35650
+ buildStateCounties(state, savedCounties) {
35651
+ return (this.usCoordinates?.locations ?? [])
35652
+ .filter((l) => l.state?.trim() === state && l.county)
35653
+ .sort((a, b) => a.county.localeCompare(b.county))
35654
+ .map((l) => ({
35655
+ stateCode: state,
35656
+ countyName: l.county,
35657
+ latitude: l.latitude,
35658
+ longitude: l.longitude,
35659
+ fillColor: '#E6E6E6',
35660
+ strokeColor: '#fff',
35661
+ checked: savedCounties?.has(l.county.trim().toLowerCase()) ?? false
35662
+ }));
35663
+ }
35655
35664
  onChangedCounty(event, stateCode, countyName, lat, lng) {
35656
35665
  const checked = event === true || event.target.checked;
35657
35666
  const current = this.filteredLocations.find(x => x.label === 'Current Coverage Area');
@@ -35716,8 +35725,7 @@ class Step2CoverageComponent {
35716
35725
  next: (res) => {
35717
35726
  console.log(res);
35718
35727
  this.prefillCoverage(res.data);
35719
- this.existingCoverageIds = (res.data || []).map((x) => x.id);
35720
- this.syncStateCheckboxes();
35728
+ this.existingByState = new Map((res.data || []).map((x) => [x.state, x]));
35721
35729
  if (this.selectedStates.length > 0) {
35722
35730
  this.store.selectedStates.set([...this.selectedStates]);
35723
35731
  }
@@ -35725,6 +35733,15 @@ class Step2CoverageComponent {
35725
35733
  error: (err) => console.error(err)
35726
35734
  });
35727
35735
  }
35736
+ // Re-reads saved row ids without touching the user's current selection
35737
+ refreshCoverageIds() {
35738
+ this.userCoverageAreaService.getUserCoverageAreaByUserId(this.userId).subscribe({
35739
+ next: (res) => {
35740
+ this.existingByState = new Map((res.data || []).map((x) => [x.state, x]));
35741
+ },
35742
+ error: (err) => console.error(err)
35743
+ });
35744
+ }
35728
35745
  syncStateCheckboxes() {
35729
35746
  this.coverages.forEach(state => {
35730
35747
  const checkedCount = state.counties.filter((c) => c.checked).length;
@@ -35744,10 +35761,11 @@ class Step2CoverageComponent {
35744
35761
  }
35745
35762
  this.coverageSave = true;
35746
35763
  const userType = this.resolvedForUser;
35764
+ const targetUserId = this.roleContextService?.tempUserContext()?.userId ?? '';
35747
35765
  const entries = this.coverages.map(s => ({
35748
35766
  forUser: userType,
35749
35767
  targetProviderId: this.providerId,
35750
- targetUserId: this.roleContextService?.tempUserContext()?.userId ?? '',
35768
+ targetUserId,
35751
35769
  providerName: this.providerName,
35752
35770
  userName: this.userDetail?.firstName + ' ' + this.userDetail?.lastName,
35753
35771
  state: s.state,
@@ -35760,44 +35778,63 @@ class Step2CoverageComponent {
35760
35778
  stateName: this.states.find((x) => x.stateCode === s.state)?.stateName,
35761
35779
  stateCode: s.state
35762
35780
  }))
35763
- }))
35764
- .filter(x => x.counties.length > 0);
35765
- if (this.existingCoverageIds.length < 0) {
35766
- this.userCoverageAreaService
35767
- .bulkDelete(this.existingCoverageIds)
35768
- .pipe(finalize(() => (this.coverageSave = false)))
35769
- .subscribe({
35770
- next: () => {
35771
- console.log('Bulk delete success');
35772
- this.initialCoverageSnapshot = JSON.stringify([]);
35773
- this.store.nextStep();
35774
- },
35775
- error: (err) => {
35776
- console.error('Bulk delete failed', err);
35777
- this.alertService.error(err?.error?.message || err?.message || 'Bulk delete failed');
35778
- }
35779
- });
35780
- return;
35781
+ }));
35782
+ const toCreate = [];
35783
+ const toUpdate = [];
35784
+ const toDeleteIds = [];
35785
+ for (const e of entries) {
35786
+ const existing = this.existingByState.get(e.state);
35787
+ if (e.counties.length === 0) {
35788
+ if (existing)
35789
+ toDeleteIds.push(existing.id);
35790
+ }
35791
+ else if (existing) {
35792
+ // Start from the loaded record so audit/concurrency fields go back unchanged
35793
+ toUpdate.push({ ...existing, ...e, id: existing.id, userId: this.userId, providerId: this.providerId });
35794
+ }
35795
+ else {
35796
+ toCreate.push(e);
35797
+ }
35781
35798
  }
35782
- if (entries.length === 0) {
35799
+ // States removed from the dropdown entirely
35800
+ this.existingByState.forEach((existing, state) => {
35801
+ if (!entries.some(e => e.state === state))
35802
+ toDeleteIds.push(existing.id);
35803
+ });
35804
+ const calls = [];
35805
+ if (toCreate.length)
35806
+ calls.push(this.userCoverageAreaService.bulkInsert(toCreate));
35807
+ if (toUpdate.length)
35808
+ calls.push(this.userCoverageAreaService.bulkUpdate(toUpdate));
35809
+ if (toDeleteIds.length)
35810
+ calls.push(this.userCoverageAreaService.bulkDelete(toDeleteIds));
35811
+ if (!calls.length) {
35783
35812
  this.coverageSave = false;
35784
35813
  this.store.nextStep();
35785
35814
  return;
35786
35815
  }
35787
- this.userCoverageAreaService
35788
- .bulkInsert(entries)
35789
- .pipe(finalize(() => (this.coverageSave = false)), catchError(err => {
35790
- console.error('Coverage save error:', err);
35791
- this.alertService.error(err?.error?.message || err?.message || 'Save failed');
35792
- return EMPTY;
35793
- }))
35794
- .subscribe((result) => {
35795
- if (result?.failed) {
35796
- this.alertService.warning(result?.message || result?.failures?.[0]?.message);
35797
- return;
35816
+ // Run one after another; parallel writes for the same user trip the server's concurrency check
35817
+ concat(...calls)
35818
+ .pipe(toArray(), finalize(() => (this.coverageSave = false)))
35819
+ .subscribe({
35820
+ next: (results) => {
35821
+ const failed = results.find(r => r?.failed);
35822
+ if (failed) {
35823
+ this.alertService.warning(failed?.message || failed?.failures?.[0]?.message);
35824
+ // Partial success is possible, so refresh ids to avoid duplicate inserts on retry
35825
+ this.refreshCoverageIds();
35826
+ return;
35827
+ }
35828
+ // Refresh ids so newly created rows are updated (not re-inserted) next time
35829
+ this.refreshCoverageIds();
35830
+ this.initialCoverageSnapshot = JSON.stringify(this.buildCoveragePayload());
35831
+ this.store.nextStep();
35832
+ },
35833
+ error: (err) => {
35834
+ console.error('Coverage save error:', err);
35835
+ this.alertService.error(err?.error?.message || err?.message || 'Save failed');
35836
+ this.refreshCoverageIds();
35798
35837
  }
35799
- this.initialCoverageSnapshot = JSON.stringify(entries);
35800
- this.store.nextStep();
35801
35838
  });
35802
35839
  }
35803
35840
  previousStep() {
@@ -35808,20 +35845,15 @@ class Step2CoverageComponent {
35808
35845
  this.selectedStates = [];
35809
35846
  data.forEach(item => {
35810
35847
  this.selectedStates.push(item.state);
35848
+ const savedCounties = new Set((item.counties || []).map((c) => c.county?.trim().toLowerCase()));
35849
+ // Show every county in the state, checking only the saved ones
35811
35850
  this.coverages.push({
35812
35851
  state: item.state,
35813
35852
  isShow: true,
35814
- counties: item.counties.map((c) => ({
35815
- stateCode: item.state,
35816
- countyName: c.county,
35817
- latitude: c.latitude,
35818
- longitude: c.longitude,
35819
- checked: true,
35820
- fillColor: '#002b49',
35821
- strokeColor: '#fff'
35822
- }))
35853
+ counties: this.buildStateCounties(item.state, savedCounties)
35823
35854
  });
35824
35855
  });
35856
+ this.syncStateCheckboxes();
35825
35857
  this.updateFilteredLocations();
35826
35858
  this.initialCoverageSnapshot = JSON.stringify(this.buildCoveragePayload());
35827
35859
  }