Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | 1x 1x 1x 1x 1x 1x 1x 1x 1x | import {bind, BindingScope, service} from '@loopback/core';
import {repository} from '@loopback/repository';
import {IsolationLevel} from '@loopback/repository';
import {BoxRepository} from '@logchain/repositories/scan/box.repository';
import {PmoRepository} from '@logchain/repositories';
import {TimelineService} from './timeline.service';
import {mapStatusToStep} from '@logchain/utils/scan';
import {HttpErrors} from '@loopback/rest';
import { TimelineStep } from '@logchain/types';
@bind({
scope: BindingScope.TRANSIENT,
})
export class BoxService {
constructor(
@repository(BoxRepository)
public boxRepo: BoxRepository,
@repository(PmoRepository)
public pmoRepo: PmoRepository,
@service(TimelineService)
private timelineService: TimelineService,
) {}
buildPMOListView(pmoIds: number[]): string {
return (pmoIds || [])
.map(x => Number(x))
.filter(x => !isNaN(x))
.join(',');
}
parsePMOListView(value?: string | null): number[] {
if (!value?.trim()) {
return [];
}
return value
.split(',')
.map(x => {
const cleaned = x.replace(/["']/g, '').trim();
return Number(cleaned);
})
.filter(x => !isNaN(x));
}
async findByPMOId(pmoId: number) {
return this.boxRepo.findByPMOId(pmoId);
}
async createBox(data: {temporaryNo?: string; shippingNo?: string; pmoIds: number[]}) {
const tx = await this.boxRepo.dataSource.beginTransaction(IsolationLevel.READ_COMMITTED);
try {
const box = await this.boxRepo.create(
{
temporary_no: data.temporaryNo ?? null,
shipping_no: data.shippingNo ?? null,
created_at: new Date(),
updated_at: new Date(),
},
{transaction: tx},
);
await tx.commit();
return {
success: true,
box,
};
} catch (e) {
await tx.rollback();
throw e;
}
}
async findBoxesByPMOListView(pmoIds: number[]) {
return this.boxRepo.findBoxesByPMOListView(pmoIds);
}
async deleteBox(rawId: any, currentDisplayedPmoIds: number[], temporaryNo?: string) {
const id = Number(String(rawId).replace(/["']/g, '').trim());
if (isNaN(id)) throw new Error('Invalid box id format');
const tx = await this.boxRepo.dataSource.beginTransaction(IsolationLevel.READ_COMMITTED);
let removedPmos: any[] = [];
try {
const box = await this.boxRepo.findById(id, undefined, {transaction: tx});
if (!box) throw new HttpErrors.NotFound('Box not found');
const resolvedTemporaryNo = temporaryNo ?? box.temporary_no ?? null;
const targetPmoIdsToRemove = (currentDisplayedPmoIds || [])
.map(x => Number(String(x).replace(/["']/g, '').trim()))
.filter(x => !isNaN(x));
const allLinkedPmos = await this.pmoRepo.find({where: {box_id: id}}, {transaction: tx});
const remainingLinkedPmos = allLinkedPmos.filter(p => !targetPmoIdsToRemove.includes(Number(p.id)));
if (targetPmoIdsToRemove.length > 0) {
removedPmos = allLinkedPmos.filter(p => targetPmoIdsToRemove.includes(Number(p.id)));
await this.pmoRepo.updateAll(
{box_id: null, shipment_id: null as any},
{box_id: id, id: {inq: targetPmoIdsToRemove}},
{transaction: tx},
);
}
if (remainingLinkedPmos.length === 0) {
await this.boxRepo.deleteById(id, {transaction: tx});
await tx.commit();
await this.createRemovedFromBoxEvents(removedPmos, resolvedTemporaryNo);
return {success: true, action: 'DELETED_BOX_COMPLETELY', message: 'Box deleted successfully.'};
} else {
await tx.commit();
await this.createRemovedFromBoxEvents(removedPmos, resolvedTemporaryNo);
return {success: true, action: 'UNLINKED_DISPLAYED_PMOS', message: 'PMOs unlinked from box successfully.'};
}
} catch (e) {
await tx.rollback();
throw e;
}
}
private async createRemovedFromBoxEvents(pmos: any[], temporaryNo: string | null) {
if (!pmos.length) return;
const userName = this.timelineService.getCurrentUserName();
await Promise.all(
pmos.map(pmo =>
this.timelineService.createEvent({
pmoId: Number(pmo.id),
stepCode: this.mapNextStep(pmo.status),
userName,
note: 'Removed from Box',
temporaryNo: temporaryNo ?? (null as any),
isRemovedFromBox: true,
}),
),
);
}
private mapNextStep(currentStatus: number): TimelineStep {
const nextStatusMap: Record<number, number> = {
1: 2,
2: 3,
3: 4,
4: 5,
5: 6,
6: 7,
};
const nextStatus = nextStatusMap[currentStatus] ?? currentStatus;
return mapStatusToStep(nextStatus);
}
}
|