This commit is contained in:
seb
2026-10-17 05:10:28 +02:00
parent a194cf6b8b
commit f8cc01baf0
25 changed files with 1034 additions and 54 deletions

View File

@@ -0,0 +1,97 @@
import {
CREATE as MSSQL_CREATE,
GET_NEXT_SORT as MSSQL_GET_NEXT_SORT,
} from '../../providers/mssql/queries/carrier.js';
import {
CREATE as SQLITE_CREATE,
GET_NEXT_SORT as SQLITE_GET_NEXT_SORT,
} from '../../providers/sqlite3/queries/carrier.js';
import { isUniqueConstraintError } from '../../providers/provider.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
function shape(row) {
return {
id: row.id,
name: row.name,
sort: row.sort,
hasPackages: Boolean(row.has_packages ?? row.hasPackages),
isReverse: Boolean(row.is_reverse ?? row.isReverse),
};
}
export default function create(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const getSortSql = isSqlite ? SQLITE_GET_NEXT_SORT : MSSQL_GET_NEXT_SORT;
const sql = isSqlite ? SQLITE_CREATE : MSSQL_CREATE;
/**
* Creates a new carrier for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - Carrier input.
* @param {string} input.name - The carrier name.
* @param {number} [input.sort] - Optional sort order (auto-increments if omitted).
* @param {boolean} [input.hasPackages=false] - Whether this carrier uses packages.
* @param {boolean} [input.isReverse=false] - Whether this carrier handles reverse shipments.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, name: string, sort: number, hasPackages: boolean, isReverse: boolean}>} The created carrier.
* @throws {Error} If name is missing, sort is invalid, or carrier already exists.
*
* @example
* const c = await store.carrier.create(googleAuthId, { name: 'DHL', hasPackages: true });
* // => { id: 1, name: 'DHL', sort: 0, hasPackages: true, isReverse: false }
*/
return async (googleAuthId, input = {}, tx) => {
const rawName = input?.name;
const cleaned = typeof rawName === 'string' ? rawName.trim() : '';
if (!cleaned) {
throw new Error('Name required');
}
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
let sort = input.sort;
if (sort === undefined || sort === null) {
const next = await runner.query(getSortSql, { companyId });
sort = next.rows[0]?.sort ?? 0;
} else if (!Number.isFinite(sort)) {
throw new Error('Invalid sort');
}
const hasPackages = Boolean(input.hasPackages ?? input.has_packages);
const isReverse = Boolean(input.isReverse ?? input.is_reverse);
try {
const result = await runner.query(sql, {
companyId,
name: cleaned,
sort,
hasPackages: hasPackages ? 1 : 0,
isReverse: isReverse ? 1 : 0,
});
const row = result.rows[0] ?? {
id: result.insertId,
company_id: companyId,
name: cleaned,
sort,
has_packages: hasPackages ? 1 : 0,
is_reverse: isReverse ? 1 : 0,
};
return shape(row);
} catch (err) {
if (
isUniqueConstraintError(err) ||
err?.number === 2627 ||
err?.number === 2601 ||
err?.code === 'SQLITE_CONSTRAINT_UNIQUE'
) {
throw new Error('Carrier already exists');
}
throw err;
}
};
}

View File

@@ -0,0 +1,62 @@
import {
DELETE_BY_COMPANY_AND_ID as MSSQL_DELETE,
CHECK_SHIPPING_USAGE as MSSQL_CHECK_USAGE,
} from '../../providers/mssql/queries/carrier.js';
import {
DELETE_BY_COMPANY_AND_ID as SQLITE_DELETE,
CHECK_SHIPPING_USAGE as SQLITE_CHECK_USAGE,
} from '../../providers/sqlite3/queries/carrier.js';
import { isForeignKeyConstraintError } from '../../providers/provider.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function del(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const deleteSql = isSqlite ? SQLITE_DELETE : MSSQL_DELETE;
const checkUsageSql = isSqlite ? SQLITE_CHECK_USAGE : MSSQL_CHECK_USAGE;
/**
* Deletes a carrier for the user's selected company, unless referenced in shipping.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The carrier ID to delete.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<void>}
* @throws {Error} If carrier not found or in use in shipping.
*
* @example
* await store.carrier.delete(googleAuthId, 1);
*/
return async (googleAuthId, id, tx) => {
const numericId = Number(id);
if (!Number.isInteger(numericId) || numericId <= 0) {
throw new Error('Carrier not found');
}
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
// Pre-check if carrier is referenced in shipping
const usage = await runner.query(checkUsageSql, { carrierId: numericId });
if (usage.rows && usage.rows.length > 0) {
throw new Error('Carrier in use');
}
try {
const result = await runner.query(deleteSql, {
id: numericId,
companyId,
});
if (!result.rowsAffected) {
throw new Error('Carrier not found');
}
} catch (err) {
if (isForeignKeyConstraintError(err)) {
throw new Error('Carrier in use');
}
throw err;
}
};
}

View File

@@ -0,0 +1,50 @@
import { GET_BY_COMPANY_AND_ID as MSSQL_GET_BY_ID } from '../../providers/mssql/queries/carrier.js';
import { GET_BY_COMPANY_AND_ID as SQLITE_GET_BY_ID } from '../../providers/sqlite3/queries/carrier.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
function shape(row) {
return {
id: row.id,
name: row.name,
sort: row.sort,
hasPackages: Boolean(row.has_packages ?? row.hasPackages),
isReverse: Boolean(row.is_reverse ?? row.isReverse),
};
}
export default function getById(provider) {
const access = resolveSelectedCompanyAccess(provider);
const sql =
provider?.dialect === 'sqlite3' ? SQLITE_GET_BY_ID : MSSQL_GET_BY_ID;
/**
* Retrieves a carrier by its ID for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The carrier ID.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, name: string, sort: number, hasPackages: boolean, isReverse: boolean}|undefined>} The carrier or undefined.
*
* @example
* const c = await store.carrier.getById(googleAuthId, 1);
* // => { id: 1, name: 'DHL', sort: 0, hasPackages: true, isReverse: false }
*/
return async (googleAuthId, id, tx) => {
const numericId = Number(id);
if (!Number.isInteger(numericId) || numericId <= 0) {
return undefined;
}
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
const result = await runner.query(sql, {
id: numericId,
companyId,
});
const row = result.rows[0];
return row ? shape(row) : undefined;
};
}

39
src/store/carrier/list.js Normal file
View File

@@ -0,0 +1,39 @@
import { LIST_BY_COMPANY as MSSQL_LIST } from '../../providers/mssql/queries/carrier.js';
import { LIST_BY_COMPANY as SQLITE_LIST } from '../../providers/sqlite3/queries/carrier.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
function shape(row) {
return {
id: row.id,
name: row.name,
sort: row.sort,
hasPackages: Boolean(row.has_packages ?? row.hasPackages),
isReverse: Boolean(row.is_reverse ?? row.isReverse),
};
}
export default function list(provider) {
const access = resolveSelectedCompanyAccess(provider);
const sql =
provider?.dialect === 'sqlite3' ? SQLITE_LIST : MSSQL_LIST;
/**
* Lists all carriers for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<Array<{id: number, name: string, sort: number, hasPackages: boolean, isReverse: boolean}>>} List of carriers.
*
* @example
* const carriers = await store.carrier.list(googleAuthId);
* // => [{ id: 1, name: 'DHL', sort: 0, hasPackages: true, isReverse: false }]
*/
return async (googleAuthId, tx) => {
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
const result = await runner.query(sql, { companyId });
return result.rows.map(shape);
};
}

View File

@@ -0,0 +1,79 @@
import {
LIST_IDS_BY_COMPANY as MSSQL_LIST_IDS_BY_COMPANY,
UPDATE_SORT as MSSQL_UPDATE_SORT,
} from '../../providers/mssql/queries/carrier.js';
import {
LIST_IDS_BY_COMPANY as SQLITE_LIST_IDS_BY_COMPANY,
UPDATE_SORT as SQLITE_UPDATE_SORT,
} from '../../providers/sqlite3/queries/carrier.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function order(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const listIdsSql = isSqlite
? SQLITE_LIST_IDS_BY_COMPANY
: MSSQL_LIST_IDS_BY_COMPANY;
const updateSortSql = isSqlite ? SQLITE_UPDATE_SORT : MSSQL_UPDATE_SORT;
/**
* Reorders the carriers of the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - The reorder input.
* @param {Array<number>} input.carrierIds - The carrier ids in their new order.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{carrierIds: Array<number>}>} The applied order.
* @throws {Error} If the order is invalid.
*
* @example
* await store.carrier.order(googleAuthId, { carrierIds: [2, 1, 3] });
*/
return async (googleAuthId, { carrierIds } = {}, tx) => {
if (!Array.isArray(carrierIds)) {
throw new Error('Invalid carrier order');
}
const runOrder = async (runner) => {
const { companyId } = await access(googleAuthId, runner);
const items = await runner.query(listIdsSql, { companyId });
const existing = new Set(items.rows.map((r) => r.id));
if (carrierIds.length !== existing.size) {
throw new Error('Invalid carrier order');
}
const seen = new Set();
for (const carrierId of carrierIds) {
if (
!Number.isFinite(carrierId) ||
!existing.has(carrierId) ||
seen.has(carrierId)
) {
throw new Error('Invalid carrier order');
}
seen.add(carrierId);
}
for (let sort = 0; sort < carrierIds.length; sort++) {
await runner.query(updateSortSql, {
id: carrierIds[sort],
companyId,
sort,
});
}
return { carrierIds };
};
if (tx) {
return runOrder(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(runOrder);
}
return runOrder(makeRunner(provider));
};
}

122
src/store/carrier/update.js Normal file
View File

@@ -0,0 +1,122 @@
import {
GET_BY_COMPANY_AND_ID as MSSQL_GET_BY_ID,
UPDATE as MSSQL_UPDATE,
} from '../../providers/mssql/queries/carrier.js';
import {
GET_BY_COMPANY_AND_ID as SQLITE_GET_BY_ID,
UPDATE as SQLITE_UPDATE,
} from '../../providers/sqlite3/queries/carrier.js';
import { isUniqueConstraintError } from '../../providers/provider.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
function shape(row) {
return {
id: row.id,
name: row.name,
sort: row.sort,
hasPackages: Boolean(row.has_packages ?? row.hasPackages),
isReverse: Boolean(row.is_reverse ?? row.isReverse),
};
}
export default function update(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const getSql = isSqlite ? SQLITE_GET_BY_ID : MSSQL_GET_BY_ID;
const updateSql = isSqlite ? SQLITE_UPDATE : MSSQL_UPDATE;
/**
* Updates a carrier name, sort order, hasPackages, and/or isReverse for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The carrier ID to update.
* @param {object} input - Update payload.
* @param {string} [input.name] - The new carrier name.
* @param {number} [input.sort] - The new sort order.
* @param {boolean} [input.hasPackages] - Whether carrier uses packages.
* @param {boolean} [input.isReverse] - Whether carrier handles reverse shipments.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, name: string, sort: number, hasPackages: boolean, isReverse: boolean}>} The updated carrier.
* @throws {Error} If carrier not found, name is empty, sort is invalid, or carrier already exists.
*
* @example
* const updated = await store.carrier.update(googleAuthId, 1, { name: 'DHL Express', hasPackages: true });
* // => { id: 1, name: 'DHL Express', sort: 0, hasPackages: true, isReverse: false }
*/
return async (googleAuthId, id, input = {}, tx) => {
const numericId = Number(id);
if (!Number.isInteger(numericId) || numericId <= 0) {
throw new Error('Carrier not found');
}
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
const existing = await runner.query(getSql, { id: numericId, companyId });
const current = existing.rows[0];
if (!current) {
throw new Error('Carrier not found');
}
let name = current.name;
if (input.name !== undefined) {
const cleaned = typeof input.name === 'string' ? input.name.trim() : '';
if (!cleaned) {
throw new Error('Name required');
}
name = cleaned;
}
let sort = current.sort;
if (input.sort !== undefined) {
if (!Number.isFinite(input.sort)) {
throw new Error('Invalid sort');
}
sort = input.sort;
}
const hasPackagesVal = input.hasPackages !== undefined
? input.hasPackages
: input.has_packages;
const hasPackages = hasPackagesVal !== undefined
? Boolean(hasPackagesVal)
: Boolean(current.has_packages ?? current.hasPackages);
const isReverseVal = input.isReverse !== undefined
? input.isReverse
: input.is_reverse;
const isReverse = isReverseVal !== undefined
? Boolean(isReverseVal)
: Boolean(current.is_reverse ?? current.isReverse);
try {
await runner.query(updateSql, {
id: numericId,
companyId,
name,
sort,
hasPackages: hasPackages ? 1 : 0,
isReverse: isReverse ? 1 : 0,
});
return shape({
id: numericId,
name,
sort,
has_packages: hasPackages ? 1 : 0,
is_reverse: isReverse ? 1 : 0,
});
} catch (err) {
if (
isUniqueConstraintError(err) ||
err?.number === 2627 ||
err?.number === 2601 ||
err?.code === 'SQLITE_CONSTRAINT_UNIQUE'
) {
throw new Error('Carrier already exists');
}
throw err;
}
};
}

View File

@@ -28,6 +28,7 @@ export function shapeShipping(row) {
manifestId: row.manifest_id ?? row.manifestId,
number: row.number,
trackingCode: row.tracking_code ?? row.trackingCode ?? null,
carrierId: row.carrier_id ?? row.carrierId,
carrier: row.carrier ?? null,
type: row.type,
state: row.state,

View File

@@ -28,20 +28,20 @@ const SQL_LINE_STATS = `
const SQL_SHIPPING_STATS_SQLITE = `
SELECT
COUNT(id) AS package_count,
(SELECT carrier FROM shipping WHERE manifest_id = @manifestId AND carrier IS NOT NULL AND TRIM(carrier) != '' ORDER BY id ASC LIMIT 1) AS carrier,
(SELECT tracking_code FROM shipping WHERE manifest_id = @manifestId AND tracking_code IS NOT NULL AND TRIM(tracking_code) != '' ORDER BY id ASC LIMIT 1) AS tracking_code
FROM shipping
WHERE manifest_id = @manifestId
COUNT(s.id) AS package_count,
(SELECT c.name FROM shipping s INNER JOIN carrier c ON c.id = s.carrier_id WHERE s.manifest_id = @manifestId AND c.name IS NOT NULL AND TRIM(c.name) != '' ORDER BY s.id ASC LIMIT 1) AS carrier,
(SELECT s.tracking_code FROM shipping s WHERE s.manifest_id = @manifestId AND s.tracking_code IS NOT NULL AND TRIM(s.tracking_code) != '' ORDER BY s.id ASC LIMIT 1) AS tracking_code
FROM shipping s
WHERE s.manifest_id = @manifestId
`;
const SQL_SHIPPING_STATS_MSSQL = `
SELECT
COUNT(id) AS package_count,
(SELECT TOP 1 carrier FROM shipping WHERE manifest_id = @manifestId AND carrier IS NOT NULL AND LTRIM(RTRIM(carrier)) != '' ORDER BY id ASC) AS carrier,
(SELECT TOP 1 tracking_code FROM shipping WHERE manifest_id = @manifestId AND tracking_code IS NOT NULL AND LTRIM(RTRIM(tracking_code)) != '' ORDER BY id ASC) AS tracking_code
FROM shipping
WHERE manifest_id = @manifestId
COUNT(s.id) AS package_count,
(SELECT TOP 1 c.name FROM shipping s INNER JOIN carrier c ON c.id = s.carrier_id WHERE s.manifest_id = @manifestId AND c.name IS NOT NULL AND LTRIM(RTRIM(c.name)) != '' ORDER BY s.id ASC) AS carrier,
(SELECT TOP 1 s.tracking_code FROM shipping s WHERE s.manifest_id = @manifestId AND s.tracking_code IS NOT NULL AND LTRIM(RTRIM(s.tracking_code)) != '' ORDER BY s.id ASC) AS tracking_code
FROM shipping s
WHERE s.manifest_id = @manifestId
`;
const SQL_ORDER_BY_NUMBER_SQLITE = `

View File

@@ -5,13 +5,14 @@ import stockShip from '../stock/ship.js';
import makeRunner from '../runner.js';
import { syncShipmentSummary } from '../shipment/syncSummary.js';
function shape(row) {
function shape(row, carrierName) {
return {
id: row.id,
manifestId: row.manifest_id ?? row.manifestId,
number: row.number,
trackingCode: row.tracking_code ?? row.trackingCode ?? null,
carrier: row.carrier ?? null,
carrierId: row.carrier_id ?? row.carrierId,
carrier: carrierName ?? row.carrier ?? null,
type: row.type,
state: row.state,
};
@@ -32,24 +33,24 @@ export default function create(provider) {
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - Shipping input.
* @param {number} input.manifestId - The shipment manifest id.
* @param {number} input.carrierId - The carrier id.
* @param {string} input.type - Shipping type.
* @param {string} input.state - Shipping state ('shipped' or other).
* @param {string} [input.trackingCode] - Carrier tracking code.
* @param {string} [input.carrier] - Carrier name.
* @param {number} [input.warehouseId] - Optional warehouse ID for stock deduction (defaults to company's first warehouse).
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, manifestId: number, number: string, trackingCode: string|null, carrier: string|null, type: string, state: string}>}
* @throws {Error} If manifest is not found, manifest is not a shipment, or input is invalid.
* @returns {Promise<{id: number, manifestId: number, number: string, trackingCode: string|null, carrierId: number, carrier: string|null, type: string, state: string}>}
* @throws {Error} If manifest is not found, carrier is not found, manifest is not a shipment, or input is invalid.
*
* @example
* await store.shipping.create(googleAuthId, { manifestId: 42, type: 'parcel', state: 'shipped', trackingCode: '1Z999', carrier: 'UPS' });
* await store.shipping.create(googleAuthId, { manifestId: 42, carrierId: 1, type: 'parcel', state: 'shipped', trackingCode: '1Z999' });
*/
return async (googleAuthId, input = {}, tx) => {
const manifestId = requireId(input.manifestId, 'Manifest');
const carrierId = requireId(input.carrierId, 'Carrier');
const type = requireText(input.type, 'Shipping type');
const state = requireText(input.state, 'Shipping state');
const trackingCode = input.trackingCode ? String(input.trackingCode).trim() : null;
const carrier = input.carrier ? String(input.carrier).trim() : null;
const isShipped = state.trim().toLowerCase() === 'shipped';
const run = async (runner, innerTx) => {
@@ -68,6 +69,16 @@ export default function create(provider) {
throw new Error('Manifest is not a shipment');
}
// Verify carrier exists and belongs to company
const carrierRes = await runner.query(q.CHECK_CARRIER, {
carrierId,
companyId,
});
const carrierRow = carrierRes.rows[0];
if (!carrierRow) {
throw new Error('Carrier not found');
}
let alreadyShipped = false;
if (isShipped) {
// Check if shipment was already marked shipped by an existing package
@@ -83,7 +94,7 @@ export default function create(provider) {
manifestId,
number,
trackingCode,
carrier,
carrierId,
type,
state,
});
@@ -92,7 +103,7 @@ export default function create(provider) {
manifest_id: manifestId,
number,
tracking_code: trackingCode,
carrier,
carrier_id: carrierId,
type,
state,
};
@@ -146,7 +157,7 @@ export default function create(provider) {
await syncShipmentSummary(runner, { companyId, manifestId });
return shape(row);
return shape(row, carrierRow.name);
};
if (tx) {

View File

@@ -31,6 +31,7 @@ export default function listByManifestId(provider) {
manifestId: row.manifest_id,
number: row.number,
trackingCode: row.tracking_code ?? null,
carrierId: row.carrier_id,
carrier: row.carrier ?? null,
type: row.type,
state: row.state,

View File

@@ -4,13 +4,14 @@ import stockShip from '../stock/ship.js';
import makeRunner from '../runner.js';
import { syncShipmentSummary } from '../shipment/syncSummary.js';
function shape(row) {
function shape(row, carrierName) {
return {
id: row.id,
manifestId: row.manifest_id ?? row.manifestId,
number: row.number,
trackingCode: row.tracking_code ?? row.trackingCode ?? null,
carrier: row.carrier ?? null,
carrierId: row.carrier_id ?? row.carrierId,
carrier: carrierName ?? row.carrier ?? null,
type: row.type,
state: row.state,
};
@@ -37,11 +38,11 @@ export default function update(provider) {
* @param {string} [patch.type] - Shipping type.
* @param {string} [patch.state] - Shipping state.
* @param {string} [patch.trackingCode] - Carrier tracking code.
* @param {string} [patch.carrier] - Carrier name.
* @param {number} [patch.carrierId] - Carrier ID.
* @param {number} [patch.warehouseId] - Warehouse ID if transitioning to 'shipped'.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, manifestId: number, number: string, trackingCode: string|null, carrier: string|null, type: string, state: string}>}
* @throws {Error} If shipping is not found, cannot be undone, or manifest not found.
* @returns {Promise<{id: number, manifestId: number, number: string, trackingCode: string|null, carrierId: number, carrier: string|null, type: string, state: string}>}
* @throws {Error} If shipping is not found, carrier is not found, cannot be undone, or manifest not found.
*/
return async (googleAuthId, id, patch = {}, tx) => {
const shippingId = requireId(id, 'Shipping');
@@ -63,7 +64,9 @@ export default function update(provider) {
const hasState = Object.prototype.hasOwnProperty.call(patch, 'state');
const hasType = Object.prototype.hasOwnProperty.call(patch, 'type');
const hasTrackingCode = Object.prototype.hasOwnProperty.call(patch, 'trackingCode');
const hasCarrier = Object.prototype.hasOwnProperty.call(patch, 'carrier');
const hasCarrierId =
Object.prototype.hasOwnProperty.call(patch, 'carrierId') ||
Object.prototype.hasOwnProperty.call(patch, 'carrier_id');
if (isCurrentlyShipped) {
if (
@@ -83,9 +86,22 @@ export default function update(provider) {
const nextTrackingCode = hasTrackingCode
? (patch.trackingCode ? String(patch.trackingCode).trim() : null)
: (current.tracking_code ?? current.trackingCode ?? null);
const nextCarrier = hasCarrier
? (patch.carrier ? String(patch.carrier).trim() : null)
: (current.carrier ?? null);
let nextCarrierId = current.carrierId ?? current.carrier_id;
let nextCarrierName = current.carrier ?? null;
if (hasCarrierId) {
const rawCarrierId = patch.carrierId ?? patch.carrier_id;
nextCarrierId = requireId(rawCarrierId, 'Carrier');
const carrierRes = await runner.query(q.CHECK_CARRIER, {
carrierId: nextCarrierId,
companyId,
});
const carrierRow = carrierRes.rows[0];
if (!carrierRow) {
throw new Error('Carrier not found');
}
nextCarrierName = carrierRow.name;
}
const willBeShipped =
!isCurrentlyShipped &&
String(nextState).trim().toLowerCase() === 'shipped';
@@ -153,7 +169,7 @@ export default function update(provider) {
type: nextType,
state: nextState,
trackingCode: nextTrackingCode,
carrier: nextCarrier,
carrierId: nextCarrierId,
});
const updatedRow = updateRes.rows[0] ?? {
@@ -161,14 +177,14 @@ export default function update(provider) {
manifest_id: current.manifestId,
number: current.number,
tracking_code: nextTrackingCode,
carrier: nextCarrier,
carrier_id: nextCarrierId,
type: nextType,
state: nextState,
};
await syncShipmentSummary(runner, { companyId, manifestId: current.manifestId });
return shape(updatedRow);
return shape(updatedRow, nextCarrierName);
};
if (tx) {