Skip to content

Commit

Permalink
Merge pull request #3462 from ita-social-projects/bugfix/#5446-order-…
Browse files Browse the repository at this point in the history
…data-update

[Bugfix] #5446 order data update
  • Loading branch information
hnativlyubomyr authored Nov 25, 2024
2 parents 37cdaeb + cb9e77a commit aa7fa14
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 94 deletions.
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 });
} 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
@@ -1,4 +1,4 @@
import { Component, OnInit, OnDestroy, AfterContentChecked, ChangeDetectorRef, ViewChild, HostListener } from '@angular/core';
import { Component, OnInit, OnDestroy, AfterContentChecked, ChangeDetectorRef, HostListener } from '@angular/core';
import { FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { formatDate } from '@angular/common';
Expand All @@ -7,19 +7,16 @@ 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';
import {
IAddressExportDetails,
IEmployee,
IExportDetails,
IGeneralOrderInfo,
IOrderDetails,
IOrderInfo,
IOrderStatusInfo,
IPaymentInfo,
IResponsiblePersons,
IUpdateResponsibleEmployee,
IUserInfo,
Expand All @@ -30,16 +27,15 @@ import {
ReturnMoneyOrBonuses
} from '../../models/ubs-admin.interface';
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 All @@ -52,7 +48,6 @@ export class UbsAdminOrderComponent implements OnInit, OnDestroy, AfterContentCh
private destroy$: Subject<boolean> = new Subject<boolean>();
isOrderCancelledAfterFormed: boolean;
orderForm: FormGroup;
isDataLoaded = false;
orderId: number;
orderInfo: IOrderInfo;
generalInfo: IGeneralOrderInfo;
Expand Down Expand Up @@ -96,18 +91,17 @@ export class UbsAdminOrderComponent implements OnInit, OnDestroy, AfterContentCh
return this.orderForm.controls.addressExportDetailsDto as FormControl;
}
constructor(
private translate: TranslateService,
private localStorageService: LocalStorageService,
private adminTableService: AdminTableService,
private fb: FormBuilder,
private dialog: MatDialog,
private route: ActivatedRoute,
private router: Router,
private changeDetector: ChangeDetectorRef,
private store: Store<IAppState>,
private orderService: OrderService,
private matSnackBar: MatSnackBarComponent,
private googleScript: GoogleScript,
private readonly translate: TranslateService,
private readonly localStorageService: LocalStorageService,
private readonly adminTableService: AdminTableService,
private readonly fb: FormBuilder,
private readonly dialog: MatDialog,
private readonly route: ActivatedRoute,
private readonly router: Router,
private readonly changeDetector: ChangeDetectorRef,
private readonly store: Store<IAppState>,
private readonly actions$: Actions,
private readonly orderService: OrderService,
public ubsAdminEmployeeService: UbsAdminEmployeeService,
public unsavedChangesGuard: UnsavedChangesGuard
) {}
Expand Down Expand Up @@ -508,66 +502,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 +530,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) {
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
22 changes: 16 additions & 6 deletions src/app/ubs/ubs-admin/services/order.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import {
Expand All @@ -10,7 +10,8 @@ import {
ResponsibleEmployee,
INotTakenOutReason,
NotTakenOutReasonImages,
IOrderInfo
IOrderInfo,
IBigOrderTableOrderInfo
} from '../models/ubs-admin.interface';
import { environment } from '@environment/environment';
import { IViolation } from '../models/violation.model';
Expand Down Expand Up @@ -97,7 +98,12 @@ export class OrderService {
return this.http.get<IOrderInfo>(`${this.backend}/management/get-data-for-order/${orderId}`);
}

updateOrderInfo(orderId: number, lang: string, data: object, images?: NotTakenOutReasonImages[]) {
updateOrderInfo(
orderId: number,
lang: string,
data: object,
images?: NotTakenOutReasonImages[]
): Observable<HttpResponse<IBigOrderTableOrderInfo>> {
const formData: FormData = new FormData();
formData.append('updateOrderPageAdminDto', JSON.stringify(data));

Expand All @@ -107,9 +113,13 @@ export class OrderService {
});
}

return this.http.patch(`${this.backend}/management/update-order-page-admin-info/${orderId}?language=${lang}`, formData, {
observe: 'response'
});
return this.http.patch<IBigOrderTableOrderInfo>(
`${this.backend}/management/update-order-page-admin-info/${orderId}?language=${lang}`,
formData,
{
observe: 'response'
}
);
}

getIsOrderCancelledAfterFormed(orderId: number): Observable<boolean> {
Expand Down

0 comments on commit aa7fa14

Please sign in to comment.