Skip to content

Commit de35251

Browse files
committed
feat(standalone variant actions): update publish and unpublish action handling
1 parent 6a34e05 commit de35251

2 files changed

Lines changed: 165 additions & 41 deletions

File tree

packages/sync-actions/src/standalone-variants/standalone-variant-actions.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
export type StandaloneVariant = {
1212
id?: string | number;
1313
key?: string;
14+
published?: boolean;
1415
sku?: string;
1516
attributes?: Attribute[];
1617
images?: Image[];
@@ -96,16 +97,33 @@ function _buildSkuAction(
9697
return null;
9798
}
9899

100+
function _buildPublishAction(
101+
oldVariant: StandaloneVariant,
102+
newVariant: StandaloneVariant
103+
): StandaloneVariantUpdateAction | null {
104+
// publish: oldObj.published is not true AND newObj.published is true
105+
if (oldVariant.published !== true && newVariant.published === true) {
106+
return { action: 'publish' };
107+
}
108+
109+
// unpublish: oldObj.published is true AND newObj.published is not true
110+
if (oldVariant.published === true && newVariant.published !== true) {
111+
return { action: 'unpublish' };
112+
}
113+
114+
return null;
115+
}
116+
99117
/**
100-
* Maps base actions (setKey, setSku) for standalone variants.
118+
* Maps base actions (setKey, setSku, publish, unpublish) for standalone variants.
101119
* Analyzes the diff between old and new variant and generates appropriate actions.
102120
*
103121
* Accepts flat variant objects directly (not wrapped in a variants array).
104122
*/
105123
export function actionsMapBase(
106124
diff: Delta,
107125
oldVariant: StandaloneVariant,
108-
_newVariant: StandaloneVariant,
126+
newVariant: StandaloneVariant,
109127
config: SyncActionConfig = {}
110128
): Array<StandaloneVariantUpdateAction> {
111129
const actions: Array<StandaloneVariantUpdateAction> = [];
@@ -124,6 +142,11 @@ export function actionsMapBase(
124142
actions.push(skuAction);
125143
}
126144

145+
const publishAction = _buildPublishAction(oldVariant, newVariant);
146+
if (publishAction) {
147+
actions.push(publishAction);
148+
}
149+
127150
return actions;
128151
}
129152

@@ -183,23 +206,3 @@ export function actionsMapAssets(
183206
return [_buildStagedAction('setAssets', { assets }, config)];
184207
}
185208

186-
/**
187-
* Builds a publish action for standalone variants.
188-
*/
189-
export function buildPublishAction(): StandaloneVariantUpdateAction {
190-
return { action: 'publish' };
191-
}
192-
193-
/**
194-
* Builds an unpublish action for standalone variants.
195-
*/
196-
export function buildUnpublishAction(): StandaloneVariantUpdateAction {
197-
return { action: 'unpublish' };
198-
}
199-
200-
/**
201-
* Builds a removeStagedChanges action for standalone variants.
202-
*/
203-
export function buildRemoveStagedChangesAction(): StandaloneVariantUpdateAction {
204-
return { action: 'removeStagedChanges' };
205-
}

packages/sync-actions/test/standalone-variants-sync.spec.ts

Lines changed: 140 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import createStandaloneVariantsSync, {
44
} from '../src/standalone-variants/standalone-variants';
55
import {
66
convertAttributeToUpdateActionShape,
7-
buildPublishAction,
8-
buildUnpublishAction,
9-
buildRemoveStagedChangesAction,
107
StandaloneVariant,
118
} from '../src/standalone-variants/standalone-variant-actions';
129
import { SyncAction } from '../src/utils/types';
@@ -1197,25 +1194,149 @@ describe('Actions', () => {
11971194
});
11981195
});
11991196

1200-
describe('Lifecycle actions', () => {
1201-
describe('buildPublishAction', () => {
1202-
test('should build publish action', () => {
1203-
const action = buildPublishAction();
1204-
expect(action).toEqual({ action: 'publish' });
1205-
});
1197+
describe('Publish/Unpublish actions', () => {
1198+
let standaloneVariantSync: SyncAction<
1199+
StandaloneVariant,
1200+
StandaloneVariantUpdateAction
1201+
>;
1202+
1203+
beforeEach(() => {
1204+
standaloneVariantSync = createStandaloneVariantsSync();
12061205
});
12071206

1208-
describe('buildUnpublishAction', () => {
1209-
test('should build unpublish action', () => {
1210-
const action = buildUnpublishAction();
1211-
expect(action).toEqual({ action: 'unpublish' });
1212-
});
1207+
test('should build `publish` action when published changes from false to true', () => {
1208+
const before: StandaloneVariant = {
1209+
id: '123',
1210+
key: 'key-1',
1211+
sku: 'sku-1',
1212+
published: false,
1213+
};
1214+
const now: StandaloneVariant = {
1215+
id: '123',
1216+
key: 'key-1',
1217+
sku: 'sku-1',
1218+
published: true,
1219+
};
1220+
1221+
const actual = standaloneVariantSync.buildActions(now, before);
1222+
1223+
expect(actual).toEqual([{ action: 'publish' }]);
12131224
});
12141225

1215-
describe('buildRemoveStagedChangesAction', () => {
1216-
test('should build removeStagedChanges action', () => {
1217-
const action = buildRemoveStagedChangesAction();
1218-
expect(action).toEqual({ action: 'removeStagedChanges' });
1219-
});
1226+
test('should build `publish` action when published changes from undefined to true', () => {
1227+
const before: StandaloneVariant = {
1228+
id: '123',
1229+
key: 'key-1',
1230+
sku: 'sku-1',
1231+
};
1232+
const now: StandaloneVariant = {
1233+
id: '123',
1234+
key: 'key-1',
1235+
sku: 'sku-1',
1236+
published: true,
1237+
};
1238+
1239+
const actual = standaloneVariantSync.buildActions(now, before);
1240+
1241+
expect(actual).toEqual([{ action: 'publish' }]);
1242+
});
1243+
1244+
test('should build `unpublish` action when published changes from true to false', () => {
1245+
const before: StandaloneVariant = {
1246+
id: '123',
1247+
key: 'key-1',
1248+
sku: 'sku-1',
1249+
published: true,
1250+
};
1251+
const now: StandaloneVariant = {
1252+
id: '123',
1253+
key: 'key-1',
1254+
sku: 'sku-1',
1255+
published: false,
1256+
};
1257+
1258+
const actual = standaloneVariantSync.buildActions(now, before);
1259+
1260+
expect(actual).toEqual([{ action: 'unpublish' }]);
1261+
});
1262+
1263+
test('should build `unpublish` action when published changes from true to undefined', () => {
1264+
const before: StandaloneVariant = {
1265+
id: '123',
1266+
key: 'key-1',
1267+
sku: 'sku-1',
1268+
published: true,
1269+
};
1270+
const now: StandaloneVariant = {
1271+
id: '123',
1272+
key: 'key-1',
1273+
sku: 'sku-1',
1274+
};
1275+
1276+
const actual = standaloneVariantSync.buildActions(now, before);
1277+
1278+
expect(actual).toEqual([{ action: 'unpublish' }]);
1279+
});
1280+
1281+
test('should not build publish/unpublish action when published is unchanged (both true)', () => {
1282+
const before: StandaloneVariant = {
1283+
id: '123',
1284+
key: 'key-1',
1285+
sku: 'sku-1',
1286+
published: true,
1287+
};
1288+
const now: StandaloneVariant = {
1289+
id: '123',
1290+
key: 'key-1',
1291+
sku: 'sku-1',
1292+
published: true,
1293+
};
1294+
1295+
const actual = standaloneVariantSync.buildActions(now, before);
1296+
1297+
expect(actual).toEqual([]);
1298+
});
1299+
1300+
test('should not build publish/unpublish action when published is unchanged (both false)', () => {
1301+
const before: StandaloneVariant = {
1302+
id: '123',
1303+
key: 'key-1',
1304+
sku: 'sku-1',
1305+
published: false,
1306+
};
1307+
const now: StandaloneVariant = {
1308+
id: '123',
1309+
key: 'key-1',
1310+
sku: 'sku-1',
1311+
published: false,
1312+
};
1313+
1314+
const actual = standaloneVariantSync.buildActions(now, before);
1315+
1316+
expect(actual).toEqual([]);
1317+
});
1318+
1319+
test('should build publish action along with other changes', () => {
1320+
const before: StandaloneVariant = {
1321+
id: '123',
1322+
key: 'key-1',
1323+
sku: 'old-sku',
1324+
published: false,
1325+
};
1326+
const now: StandaloneVariant = {
1327+
id: '123',
1328+
key: 'key-1',
1329+
sku: 'new-sku',
1330+
published: true,
1331+
};
1332+
1333+
const actual = standaloneVariantSync.buildActions(now, before);
1334+
1335+
expect(actual).toEqual(
1336+
expect.arrayContaining([
1337+
{ action: 'setSku', sku: 'new-sku', staged: true },
1338+
{ action: 'publish' },
1339+
])
1340+
);
12201341
});
12211342
});

0 commit comments

Comments
 (0)