Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] #5446 order data update #3462

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/app/store/actions/bigOrderTable.actions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createAction, props } from '@ngrx/store';
import { GetLocations } from 'src/app/store/actions/tariff.actions';
import {
IBigOrderTable,
IBigOrderTableOrderInfo,
IBigOrderTableParams,
IFilter,
IFilters,
ILocationDetails,
IOrdersViewParameters
IOrdersViewParameters,
NotTakenOutReasonImages
} from 'src/app/ubs/ubs-admin/models/ubs-admin.interface';

export enum BigOrderTableActions {
Expand All @@ -21,6 +22,8 @@ export enum BigOrderTableActions {
ChangingOrderData = '[BigOrderTable] Changing Order Data',
ChangingOrderPaymentStatus = '[BigOrderTable] Changing Order Payment Status',
ChangingOrderDataSuccess = '[BigOrderTable] Changing Order Data Success',
UpdateOrderInfo = '[BigOrderTable] Update Order Info',
UpdateOrderInfoSuccess = '[BigOrderTable] Update Order Info Success',
ReceivedFailure = '[BigOrderTable] Received Failure',

AddFilters = '[BigOrderTable] Add Filters',
Expand Down Expand Up @@ -83,6 +86,21 @@ export const ChangingOrderDataSuccess = createAction(
props<{ orderId: number[]; columnName: string; newValue: string }>()
);

export const UpdateOrderInfo = createAction(
BigOrderTableActions.UpdateOrderInfo,
props<{
orderId: number;
updatedOrder: IBigOrderTableOrderInfo;
currentLanguage: string;
notTakenOutReasonImages?: NotTakenOutReasonImages[];
}>()
);

export const UpdateOrderInfoSuccess = createAction(
BigOrderTableActions.UpdateOrderInfoSuccess,
props<{ updatedOrder: IBigOrderTableOrderInfo }>()
);

export const AddFiltersAction = createAction(BigOrderTableActions.AddFilters, props<{ filters: IFilters; fetchTable: boolean }>());
export const AddFilterMultiAction = createAction(BigOrderTableActions.AddFilterMulti, props<{ filter: IFilter; fetchTable: boolean }>());
export const RemoveFilter = createAction(BigOrderTableActions.RemoveFilter, props<{ filter: IFilter; fetchTable: boolean }>());
Expand Down
32 changes: 30 additions & 2 deletions src/app/store/effects/bigOrderTable.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { select, Store } from '@ngrx/store';
import { of } from 'rxjs';
import { catchError, concatMap, map, mergeMap, switchMap, tap, withLatestFrom } from 'rxjs/operators';
import { filtersSelector } from 'src/app/store/selectors/big-order-table.selectors';
import { MatSnackBarComponent } from '@global-errors/mat-snack-bar/mat-snack-bar.component';
import {
IBigOrderTable,
IBigOrderTableOrderInfo,
IBigOrderTableParams,
ILocationDetails,
IOrdersViewParameters
Expand All @@ -33,7 +35,9 @@ import {
RemoveFilter,
SaveFiltersAction,
SetColumnToDisplay,
SetColumnToDisplaySuccess
SetColumnToDisplaySuccess,
UpdateOrderInfo,
UpdateOrderInfoSuccess
} from '../actions/bigOrderTable.actions';

@Injectable()
Expand All @@ -43,7 +47,8 @@ export class BigOrderTableEffects {
private adminTableService: AdminTableService,
private orderService: OrderService,
private localStorageService: LocalStorageService,
private store: Store
private store: Store,
private snackBar: MatSnackBarComponent
) {}

getColumnToDisplay = createEffect(() => {
Expand Down Expand Up @@ -129,6 +134,29 @@ export class BigOrderTableEffects {
);
});

updateOrderInfo = createEffect(() =>
this.actions.pipe(
ofType(UpdateOrderInfo),
mergeMap((action) =>
this.orderService.updateOrderInfo(action.orderId, action.currentLanguage, action.updatedOrder, action.notTakenOutReasonImages).pipe(
map((response) => {
if (response.body) {
this.snackBar.openSnackBar('changesSaved');
return UpdateOrderInfoSuccess({ updatedOrder: response.body as IBigOrderTableOrderInfo });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid explicit type casting, you can modify the response body properly:
updateOrderInfo():Observable<HttpResponse<IBigOrderTableOrderInfo>>

} else {
this.snackBar.openSnackBar('error');
return ReceivedFailure({ error: 'Failed to update order' });
}
}),
catchError((error) => {
this.snackBar.openSnackBar('error');
return of(ReceivedFailure({ error }));
})
)
)
)
);

addFilter = createEffect(
() =>
this.actions.pipe(
Expand Down
19 changes: 17 additions & 2 deletions src/app/store/reducers/bigOrderTable.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
RemoveFilter,
AddFilterMultiAction,
ClearFilters,
GetLocationsDetails,
GetLocationsDetailsSuccess
GetLocationsDetailsSuccess,
UpdateOrderInfoSuccess
} from '../actions/bigOrderTable.actions';
import { createReducer, on } from '@ngrx/store';
import { IFilters } from 'src/app/ubs/ubs-admin/models/ubs-admin.interface';
Expand Down Expand Up @@ -57,6 +57,21 @@ export const bigOrderTableReducer = createReducer(
}
})),

on(UpdateOrderInfoSuccess, (state, action) => {
if (!state.bigOrderTable || !state.bigOrderTable.content) {
return state;
}
return {
...state,
bigOrderTable: {
...state.bigOrderTable,
content: state.bigOrderTable.content.map((orderData) =>
orderData.id === action.updatedOrder.id ? { ...action.updatedOrder } : orderData
)
}
};
}),

on(ChangingOrderPaymentStatus, (state, action) => ({
...state,
bigOrderTable: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class UbsAdminOrderHistoryComponent implements OnDestroy, OnChanges, OnIn

ngOnChanges(changes: SimpleChanges): void {
const orderID = this.orderInfo.generalOrderInfo.id;
if (changes.orderInfo) {
if (changes.orderInfo && this.currentLanguage) {
this.getOrderHistory(orderID);
this.getNotTakenOutReason(orderID);
this.getOrderCancelReason(orderID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Observable, Subject } from 'rxjs';
import { take, takeUntil } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import { MatSnackBarComponent } from '@global-errors/mat-snack-bar/mat-snack-bar.component';
import { UbsAdminCancelModalComponent } from '../ubs-admin-cancel-modal/ubs-admin-cancel-modal.component';
import { OrderService } from '../../services/order.service';
import { LocalStorageService } from '@global-service/localstorage/local-storage.service';
Expand All @@ -30,16 +29,15 @@ import {
ReturnMoneyOrBonuses
} from '../../models/ubs-admin.interface';
Copy link
Contributor

@Lokankara Lokankara Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused imports specifier IPaymentInfo, IAddressExportDetails, ViewChild
Unused field isDataLoaded
Also pay attention on sonar issues

import { IAppState } from 'src/app/store/state/app.state';
import { ChangingOrderData } from 'src/app/store/actions/bigOrderTable.actions';
import { UpdateOrderInfo, UpdateOrderInfoSuccess } from 'src/app/store/actions/bigOrderTable.actions';
import { Patterns } from 'src/assets/patterns/patterns';
import { GoogleScript } from 'src/assets/google-script/google-script';
import { PhoneNumberValidator } from 'src/app/shared/phone-validator/phone.validator';
import { OrderStatus, PaymentEnrollment } from 'src/app/ubs/ubs/order-status.enum';
import { OrderStatus } from 'src/app/ubs/ubs/order-status.enum';
import { UbsAdminEmployeeService } from '../../services/ubs-admin-employee.service';
import { AdminTableService } from '../../services/admin-table.service';
import { TableKeys } from '../../services/table-keys.enum';
import { UnsavedChangesGuard } from '@ubs/ubs-admin/unsaved-changes-guard.guard';
import { Address } from '@ubs/ubs/models/ubs.interface';
import { Actions, ofType } from '@ngrx/effects';

@Component({
selector: 'app-ubs-admin-order',
Expand Down Expand Up @@ -105,9 +103,8 @@ export class UbsAdminOrderComponent implements OnInit, OnDestroy, AfterContentCh
private router: Router,
private changeDetector: ChangeDetectorRef,
private store: Store<IAppState>,
private actions$: Actions,
private orderService: OrderService,
private matSnackBar: MatSnackBarComponent,
private googleScript: GoogleScript,
public ubsAdminEmployeeService: UbsAdminEmployeeService,
public unsavedChangesGuard: UnsavedChangesGuard
) {}
Expand Down Expand Up @@ -508,66 +505,20 @@ export class UbsAdminOrderComponent implements OnInit, OnDestroy, AfterContentCh

this.addIdForUserAndAdress(changedValues);

this.orderService
.updateOrderInfo(this.orderId, this.currentLanguage, changedValues, this.notTakenOutReasonImages)
.pipe(takeUntil(this.destroy$))
.subscribe((response) => {
this.matSnackBar.openSnackBar(response.ok ? 'changesSaved' : 'error');
if (response.ok) {
this.getOrderInfo(this.orderId);
if (changedValues?.generalOrderInfo) {
Object.keys(changedValues?.generalOrderInfo).forEach((key: string) => {
if (changedValues.generalOrderInfo[key]) {
this.postDataItem([this.orderId], key, changedValues.generalOrderInfo[key]);
}
});
}
this.updateExportDataInState(changedValues);
this.updateResponsibleEmployeeInState(changedValues);
}
});
this.statusCanceledOrDone();
}

private updateExportDataInState(changedValues: IOrderInfo) {
if (changedValues?.exportDetailsDto) {
if (this.dateExport) {
this.postDataItem([this.orderId], TableKeys.dateOfExport, this.dateExport);
}
if (this.receivingStationId) {
this.postDataItem([this.orderId], TableKeys.receivingStation, String(this.receivingStationId));
}
if (this.timeDeliveryFrom && this.timeDeliveryTo) {
this.postDataItem([this.orderId], TableKeys.timeOfExport, [this.timeDeliveryFrom, this.timeDeliveryTo].join('-'));
}
}
}
this.store.dispatch(
UpdateOrderInfo({
orderId: this.orderId,
updatedOrder: changedValues,
currentLanguage: this.currentLanguage,
notTakenOutReasonImages: this.notTakenOutReasonImages
})
);

private updateResponsibleEmployeeInState(changedValues: IOrderInfo) {
if (changedValues?.updateResponsibleEmployeeDto) {
changedValues?.updateResponsibleEmployeeDto.forEach((key) => {
switch (key.positionId) {
case 2:
this.postDataItem([this.orderId], TableKeys.responsibleCaller, String(key.employeeId));
break;
case 3:
this.postDataItem([this.orderId], TableKeys.responsibleLogicMan, String(key.employeeId));
break;
case 4:
this.postDataItem([this.orderId], TableKeys.responsibleNavigator, String(key.employeeId));
break;
case 5:
this.postDataItem([this.orderId], TableKeys.responsibleDriver, String(key.employeeId));
break;
default:
break;
}
});
}
}
this.actions$.pipe(ofType(UpdateOrderInfoSuccess), take(1)).subscribe(() => {
this.getOrderInfo(this.orderId);
});

private postDataItem(orderId: number[], columnName: string, newValue: string): void {
this.store.dispatch(ChangingOrderData({ orderData: [{ orderId, columnName, newValue }] }));
this.statusCanceledOrDone();
}

private getUpdates(formItem: FormGroup | FormArray | FormControl, changedValues: IOrderInfo, name?: string) {
Expand All @@ -582,8 +533,9 @@ export class UbsAdminOrderComponent implements OnInit, OnDestroy, AfterContentCh
for (const formControlName in formItem.controls) {
if (Object.prototype.hasOwnProperty.call(formItem.controls, formControlName)) {
const formControl = formItem.controls[formControlName];

if (formControl instanceof FormControl) {
if (formControlName === 'userInfoDto' && formControl.dirty) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeated logic for type checks and value assignment is extracted into helper methods, reducing redundancy.
Centralize the logic for dirty checks, etc
private isFormControl(item: AbstractControl): item is FormControl { return item instanceof FormControl; }

changedValues[formControlName] = formControl.value;
} else if (formControl instanceof FormControl) {
this.getUpdates(formControl, changedValues, formControlName);
} else if (formControl instanceof FormArray && formControl.dirty && formControl.controls.length > 0) {
changedValues[formControlName] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ export class UbsAdminTableComponent implements OnInit, AfterViewChecked, OnDestr
}

openOrder(id: number): void {
this.adminTableService.blockOrders([id]).subscribe();
this.router
.navigate(['ubs-admin', 'order', `${id}`])
.then(() => {})
Expand Down
Loading