-
Notifications
You must be signed in to change notification settings - Fork 0
/
createNextWeek.js
221 lines (183 loc) · 6.74 KB
/
createNextWeek.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Creates a new product based on an existing product's ID.
*
* @param {number} oldProductId - The ID of the existing product to use as a template.
* @returns {(number|null)} - The ID of the newly created product, or null if the operation failed.
*/
function createNextWeek(oldProductId) {
var meta_key = "event_next_week";
var product = getProductById(Number(oldProductId));
var existingProductId = getMetaValue(product, meta_key);
if (Number(existingProductId) === Number(oldProductId)) {
console.log("Product already exists with the same meta value:", existingProductId);
return existingProductId;
}
var nextThursday = getNextThursdayAt6PM();
var productName = "Outdoor Thursday Climbing Clan";
var dateFormatted = formatDate(nextThursday);
var newProductName = productName + " " + dateFormatted;
var newProductDate = formatDateISO(nextThursday);
// Check if an event with the same name already exists
var existingProduct = getProductByName(newProductName);
//console.log(JSON.stringify(existingProduct))
if (existingProduct && existingProduct.sku && String(existingProduct.sku) === String(newProductDate + "-Outdoor")) {
console.log("Event already exists with the name:", newProductName);
console.log(JSON.stringify(existingProduct))
return existingProduct.id;
}
var newProductId = duplicateProductInWordPress(outdoorTemplatePostID);
console.log("old:" + oldProductId);
console.log("new:" + newProductId);
console.log(meta_key);
if (newProductId) {
var success = addMetaToProduct(product, meta_key, newProductId.toString());
if (success) {
console.log("New Event ID Noted:", success);
return newProductId;
}
}
return null;
}
function getProductByName(productName) {
var encodedAuthInformation = Utilities.base64Encode(apiusername + ":" + apipassword);
var headers = { "Authorization": "Basic " + encodedAuthInformation };
var productUrl = "https://www." + apidomain + "/wp-json/wc/v3/products?search=" + encodeURIComponent(productName);
var options = { 'headers': headers };
var response = UrlFetchApp.fetch(productUrl, options);
var products = JSON.parse(response.getContentText());
if (products.length > 0) {
return products[0];
}
return null;
}
function getProductBySKU(sku) {
var encodedAuthInformation = Utilities.base64Encode(apiusername + ":" + apipassword);
var headers = { "Authorization": "Basic " + encodedAuthInformation };
var productUrl = "https://www." + apidomain + "/wp-json/wc/v3/products?sku=" + encodeURIComponent(sku);
var options = { 'headers': headers };
var response = UrlFetchApp.fetch(productUrl, options);
var products = JSON.parse(response.getContentText());
if (products.length > 0) {
return products[0];
}
return null;
}
/**
* Duplicates a product in WordPress based on a template.
*
* @param {number} templateFunction - The ID of the template product to use.
* @returns {number} - The ID of the newly created product.
*/
function duplicateProductInWordPress(templateFunction) {
var nextThursday = getNextThursdayAt6PM();
var wednesdayNoon = getWednesdayNoon();
var productName = "Outdoor Thursday Climbing Clan";
var dateFormatted = formatDate(nextThursday);
var newProductName = productName + " " + dateFormatted;
var newProductDate = formatDateISO(nextThursday);
var originalProduct = getProductById(templateFunction);
var newProduct = createDuplicateProduct(originalProduct);
updateProductFields(newProduct, newProductName, productName, newProductDate + "-Outdoor", nextThursday, wednesdayNoon);
var newProductId = sendProductToWordPress(newProduct);
return newProductId;
}
/**
* Calculates the next Thursday at 6:00 PM.
*
* @returns {Date} - The next Thursday at 6:00 PM.
*/
function getNextThursdayAt6PM() {
var nextThursday = new Date();
var dayOfWeek = nextThursday.getDay();
if (dayOfWeek >= 4) {
nextThursday.setDate(nextThursday.getDate() + (11 - dayOfWeek));
} else {
nextThursday.setDate(nextThursday.getDate() + (4 - dayOfWeek));
}
nextThursday.setHours(19, 0, 0, 0);
return nextThursday;
}
/**
* Calculates the current Wednesday at 12:00 PM (noon).
*
* @returns {Date} - The current Wednesday at 12:00 PM (noon).
*/
function getWednesdayNoon() {
var wednesdayNoon = new Date();
wednesdayNoon.setDate(wednesdayNoon.getDate() + (3 + (7 - wednesdayNoon.getDay())) % 7);
wednesdayNoon.setHours(12, 0, 0, 0);
return wednesdayNoon;
}
/**
* Formats a date in the format "MM/DD/YY".
*
* @param {Date} date - The date to format.
* @returns {string} - The formatted date.
*/
function formatDate(date) {
var day = ("0" + date.getDate()).slice(-2);
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var year = date.getFullYear().toString().slice(-2);
return month + "/" + day + "/" + year;
}
/**
* Formats a date in the ISO 8601 format (YYYY-MM-DD).
*
* @param {Date} date - The date to format.
* @returns {string} - The formatted date.
*/
function formatDateISO(date) {
return date.toISOString().split("T")[0];
}
/**
* Formats a date in the format "DD/MM/YYYY HH:mm".
*
* @param {Date} date - The date to format.
* @returns {string} - The formatted date.
*/
function formatDateWithLowercaseMeridian(date) {
var formattedHours = date.getHours().toString().padStart(2, "0");
var formattedMinutes = date.getMinutes().toString().padStart(2, "0");
return Utilities.formatDate(date, "GMT", "dd/MM/yyyy ") + formattedHours + ":" + formattedMinutes;
}
/**
* Converts a string to a URL-friendly slug.
*
* @param {string} text - The text to convert to a slug.
* @returns {string} - The slug.
*/
function slugify(text) {
return text.toLowerCase().replace(/ /g, "-").replace(/[^\w-]+/g, "-");
}
/**
* Updates the stock status of a product in WordPress.
*
* @param {number} productId - The ID of the product to update.
* @param {boolean} isInStock - True if the product is in stock, false otherwise.
*/
function updateProductStockStatus(productId, isInStock) {
if (productId !== null) {
var product = getProductById(productId);
if (product !== null) {
var stockStatus = isInStock ? 'instock' : 'outofstock';
product.stock_status = stockStatus;
updateProductInWordPress(product);
}
}
}
/**
* Marks a product as in stock.
*/
function markInStock() {
var productId = setupCell("Dashboard","B5")
var isInStock = true; // Set the stock status (true for in stock, false for out of stock)
updateProductStockStatus(productId, isInStock);
}
/**
* Marks a product as out of stock.
*/
function markOutOfStock() {
var productId = setupCell("Dashboard","B5")
var isInStock = false; // Set the stock status (true for in stock, false for out of stock)
updateProductStockStatus(productId, isInStock);
}