Clean seller metadata display names
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 40s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 40s
This commit is contained in:
@@ -42,6 +42,7 @@ const CUSTOMER_IDENTITY_CTE = `
|
||||
)
|
||||
`;
|
||||
const CUSTOMER_KEY_SQL = 'customer_key';
|
||||
const TRAILING_SELLER_ID_SQL_PATTERN = '[[:space:]]*#([0-9]+)[[:space:]]*$';
|
||||
|
||||
const getClientTokenSecret = () => (
|
||||
process.env.CLIENT_TOKEN_SECRET ||
|
||||
@@ -196,6 +197,19 @@ const normalizeSellerFilter = (value) => {
|
||||
return { type: 'any', value: normalizedValue };
|
||||
};
|
||||
|
||||
const normalizeSellerOption = (row) => {
|
||||
const rawId = String(row.id || '').trim();
|
||||
const rawName = String(row.name || '').trim();
|
||||
const idFromName = rawName.match(/#(\d+)\s*$/)?.[1] || '';
|
||||
const id = rawId || idFromName;
|
||||
const name = rawName.replace(/#\d+\s*$/, '').trim();
|
||||
|
||||
return {
|
||||
id,
|
||||
name: name || id
|
||||
};
|
||||
};
|
||||
|
||||
const appendOrderMetadataFilters = (params, filters, range = {}) => {
|
||||
const marketplace = normalizeTextFilter(range.marketplace);
|
||||
const salesChannel = normalizeTextFilter(range.canal_venda || range.canalVenda);
|
||||
@@ -703,8 +717,11 @@ const getClientFilterOptions = async () => {
|
||||
pool.query(`
|
||||
WITH seller_options AS (
|
||||
SELECT
|
||||
NULLIF(TRIM(id_vendedor), '') as id,
|
||||
NULLIF(TRIM(nome_vendedor), '') as name
|
||||
COALESCE(
|
||||
NULLIF(TRIM(id_vendedor), ''),
|
||||
substring(NULLIF(TRIM(nome_vendedor), '') from '${TRAILING_SELLER_ID_SQL_PATTERN}')
|
||||
) as id,
|
||||
NULLIF(TRIM(regexp_replace(COALESCE(nome_vendedor, ''), '${TRAILING_SELLER_ID_SQL_PATTERN}', '')), '') as name
|
||||
FROM orders
|
||||
WHERE (
|
||||
NULLIF(TRIM(id_vendedor), '') IS NOT NULL
|
||||
@@ -720,8 +737,7 @@ const getClientFilterOptions = async () => {
|
||||
const sellerOptionsByValue = new Map();
|
||||
|
||||
sellerResult.rows.forEach(row => {
|
||||
const id = row.id || '';
|
||||
const name = row.name || '';
|
||||
const { id, name } = normalizeSellerOption(row);
|
||||
const value = id ? `id:${id}` : `name:${name}`;
|
||||
if (!value || sellerOptionsByValue.has(value)) return;
|
||||
|
||||
|
||||
@@ -641,6 +641,7 @@ test('getClientFilterOptions returns all distinct order metadata options', async
|
||||
rows: [
|
||||
{ id: 'VEN-1', name: 'Maria' },
|
||||
{ id: '', name: 'Sem ID' },
|
||||
{ id: '', name: 'KEDMA DA SILVA #977226210' },
|
||||
{ id: 'VEN-1', name: 'Maria' }
|
||||
]
|
||||
};
|
||||
@@ -657,12 +658,15 @@ test('getClientFilterOptions returns all distinct order metadata options', async
|
||||
});
|
||||
assert.match(calls[0].sql, /WHERE NULLIF\(TRIM\(marketplace\), ''\) IS NOT NULL/);
|
||||
assert.match(calls[2].sql, /WITH seller_options AS/);
|
||||
assert.match(calls[2].sql, /substring\(NULLIF\(TRIM\(nome_vendedor\), ''\) from/);
|
||||
assert.match(calls[2].sql, /regexp_replace\(COALESCE\(nome_vendedor, ''\)/);
|
||||
assert.match(calls[2].sql, /NULLIF\(TRIM\(nome_vendedor\), ''\) IS NOT NULL/);
|
||||
assert.deepEqual(options.marketplaces, ['Mercado Livre', 'Shopee']);
|
||||
assert.deepEqual(options.salesChannels, ['Online']);
|
||||
assert.deepEqual(options.sellers, [
|
||||
{ value: 'id:VEN-1', id: 'VEN-1', name: 'Maria' },
|
||||
{ value: 'name:Sem ID', id: '', name: 'Sem ID' }
|
||||
{ value: 'name:Sem ID', id: '', name: 'Sem ID' },
|
||||
{ value: 'id:977226210', id: '977226210', name: 'KEDMA DA SILVA' }
|
||||
]);
|
||||
} finally {
|
||||
pool.query = originalQuery;
|
||||
|
||||
Reference in New Issue
Block a user