-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (72 loc) · 2.69 KB
/
index.js
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
import dotenv from 'dotenv';
import { create, Criteria } from 'shopware-admin-api-client';
import fs from 'fs';
// Main async function
async function main() {
// Load environment variables from .env file
dotenv.config();
// Validate environment variables
if (!process.env.SHOPWARE_API_URL || !process.env.SHOPWARE_API_CLIENT_ID || !process.env.SHOPWARE_API_CLIENT_SECRET) {
console.error('Missing required environment variables.');
return;
}
// Create the API client
let api = await create(process.env.SHOPWARE_API_URL, process.env.SHOPWARE_API_CLIENT_ID, process.env.SHOPWARE_API_CLIENT_SECRET);
await shopware(api)
console.log('Done.');
}
// Your code to interact with the Shopware API
async function shopware(api) {
let repository = api.create('product');
let criteria = new Criteria();
criteria.limit = 1;
criteria.addFilter(Criteria.equals('parentId', null));
// criteria.addFilter(Criteria.range('childCount', { gt: 0 }));
// criteria.addAssociation('children.cover');
while (true) {
const context = api.defaultContext();
// Optional: enable inheritance
context.inheritance = true;
let entities = await repository.search(criteria, context);
if (criteria.page < 2) {
console.log(`Entities total: ${entities.total} (limit: ${criteria.limit})`);
}
if (entities.length < 1) {
break;
}
console.log(`Page: ${criteria.page} / ${Math.ceil(entities.total / criteria.limit)}`);
for (const entity of entities) {
await handleEntity(entity)
}
// Optional: show statistics if changes are about to be saved
const { changeset, deletions } = repository.getSyncChangeset(entities);
if (changeset.length > 0 || deletions.length > 0) {
console.log(`Changesets: ${changeset.length} / Deletions: ${deletions.length}`);
// Optional: log changes to a file
for (const entity of changeset) {
const change = new Date().toISOString() + ' ' + JSON.stringify(entity.changes) + '\n';
fs.appendFileSync('changes.log', change);
}
}
// Save all entities at once (bulk update)
await repository.sync(entities, api.defaultContext());
criteria.page++;
// Optional: break after the first page
break;
}
}
// Your code to handle the entity
async function handleEntity(entity) {
console.log(entity.name);
// You can modify the entity here
// entity.name += '3';
// entity.stock = 1;
}
// Call the main function
main().catch((error) => {
// check if error.response.data.errors exists - that means it's an API error
if (error.response && error.response.data && error.response.data.errors) {
error = error.response.data.errors;
}
console.error('Error in main function:', error);
});