Files
dashlibstorage/src/store/articleproperty/delete.js
2026-10-12 17:29:29 +02:00

51 lines
1.8 KiB
JavaScript

import {
DELETE_BY_ARTICLE_AND_PROPERTY_ID as MSSQL_DELETE,
} from '../../providers/mssql/queries/articleproperty.js';
import {
DELETE_BY_ARTICLE_AND_PROPERTY_ID as SQLITE_DELETE,
} from '../../providers/sqlite3/queries/articleproperty.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
import { recordPropertyDeleteHistory } from '../articlehistory/record.js';
import makeRunner from '../runner.js';
export default function deleteArticleProperty(provider) {
const resolve = resolveArticleBySku(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const deleteSql = isSqlite ? SQLITE_DELETE : MSSQL_DELETE;
/**
* Deletes a property from an article by SKU and propertyId.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {number} propertyId - The property id.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<void>}
*/
return async (googleAuthId, sku, propertyId, tx) => {
const runner = makeRunner(provider, tx);
const { articleId, companyuserId } = await resolve(googleAuthId, sku, runner);
// Read the property name + value before deleting (for the history entry)
const propRes = await runner.query(
`SELECT ap.value, pn.name
FROM articleproperty ap
INNER JOIN propertyname pn ON pn.id = ap.property_id
WHERE ap.article_id = @articleId AND ap.property_id = @propertyId`,
{ articleId, propertyId },
);
const prop = propRes.rows[0];
await runner.query(deleteSql, { articleId, propertyId });
if (prop) {
await recordPropertyDeleteHistory(runner, {
articleId,
companyuserId,
name: prop.name,
value: prop.value,
});
}
};
}