-
Notifications
You must be signed in to change notification settings - Fork 10
/
bulk-update-listings.js
108 lines (93 loc) · 4.02 KB
/
bulk-update-listings.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// This dotenv import is required for the `.env` file to be read
require('dotenv').config();
const sharetribeIntegrationSdk = require('sharetribe-flex-integration-sdk');
// Read dry run from arguments
const dryRun = process.argv[2];
// Create rate limit handler for queries.
// NB! If you are using the script in production environment,
// you will need to use sharetribeIntegrationSdk.util.prodQueryLimiterConfig
const queryLimiter = sharetribeIntegrationSdk.util.createRateLimiter(
sharetribeIntegrationSdk.util.devQueryLimiterConfig
);
// Create rate limit handler for commands.
// NB! If you are using the script in production environment,
// you will need to use sharetribeIntegrationSdk.util.prodCommandLimiterConfig
const commandLimiter = sharetribeIntegrationSdk.util.createRateLimiter(
sharetribeIntegrationSdk.util.devCommandLimiterConfig
);
const integrationSdk = sharetribeIntegrationSdk.createInstance({
// These two env vars need to be set in the `.env` file.
clientId: process.env.SHARETRIBE_INTEGRATION_CLIENT_ID,
clientSecret: process.env.SHARETRIBE_INTEGRATION_CLIENT_SECRET,
// Pass rate limit handlers
queryLimiter: queryLimiter,
commandLimiter: commandLimiter,
// Normally you can just skip setting the base URL and just use the
// default that the `createInstance` uses. We explicitly set it here
// for local testing and development.
baseUrl: process.env.SHARETRIBE_INTEGRATION_BASE_URL || 'https://flex-integ-api.sharetribe.com',
});
// Takes `fns` array of functions and executes them sequentially.
const bulkUpdate = (fns, resolve, reject, results = []) => {
const [firstFn, ...restFns] = fns;
if (firstFn) {
firstFn()
.then(res => {
bulkUpdate(restFns, resolve, reject, [...results, res]);
})
.catch(res => {
reject([...results, res]);
});
} else {
resolve(results);
}
}
// Fetch all listings in marketplace and analyze how many of them need to be updated.
const analyze = integrationSdk.listings.query()
.then(res => {
console.log('Total listing count:', res.data.meta.totalItems)
const dayBased = res.data.data.filter(listing => {
const plan = listing.attributes.availabilityPlan;
return plan === null ||
plan.type === 'availability-plan/day';
});
console.log('Listings to migrate:', dayBased.length);
return dayBased;
});
if (dryRun === '--dry-run=false') {
analyze
.then(dayBasedListings => {
return new Promise((resolve, reject) => {
// Wrap calls to integration API in a function, that will be later
// executed by the bulkUpdate function.
const fns = dayBasedListings.map(listing => () => {
console.log('Updating listing', listing.id.uuid)
return integrationSdk.listings.update({
id: listing.id,
availabilityPlan: {
type: 'availability-plan/time',
timezone: 'Europe/Helsinki',
entries: [
{dayOfWeek: 'mon', startTime: '00:00', endTime: '00:00', seats: 1},
{dayOfWeek: 'tue', startTime: '00:00', endTime: '00:00', seats: 1},
{dayOfWeek: 'wed', startTime: '00:00', endTime: '00:00', seats: 1},
{dayOfWeek: 'thu', startTime: '00:00', endTime: '00:00', seats: 1},
{dayOfWeek: 'fri', startTime: '00:00', endTime: '00:00', seats: 1},
{dayOfWeek: 'sat', startTime: '00:00', endTime: '00:00', seats: 1},
{dayOfWeek: 'sun', startTime: '00:00', endTime: '00:00', seats: 1}
]}
});
});
// Execute
bulkUpdate(fns, resolve, reject);
});
})
.then(res => {
console.log('Successfully updated:')
console.log(res);
});
} else {
analyze.then(() => {
console.log('To execute bulk update, run this command with --dry-run=false option')
});
}