-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmembershipManagement.js
98 lines (83 loc) · 2.36 KB
/
membershipManagement.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
function updateMembershipStatus(metaKey, metaValue) {
const ui = SpreadsheetApp.getUi();
const sheet = SpreadsheetApp.getActive().getSheetByName("BCA-CIM-Proforma");
const currentRow = sheet.getActiveRange().getRowIndex();
// Validate row selection
if (!isValidRow(currentRow)) {
ui.alert("Please select a valid member row");
return;
}
// Get user details
const userDetails = getUserDetails(sheet, currentRow);
if (!userDetails.userId) {
ui.alert("No User ID Found");
return;
}
// Confirm action with user
if (!confirmAction(ui, userDetails.firstName, userDetails.userId)) {
return;
}
// Update WordPress
try {
const success = updateWordPressMembership(
userDetails.userId,
metaKey,
metaValue,
);
if (success) {
updateSheetStatus(sheet, currentRow);
return metaValue;
}
} catch (error) {
ui.alert("Error", `Failed to update: ${error.message}`, ui.ButtonSet.OK);
return `ERROR: ${error.message}`;
}
}
// Helper functions
function isValidRow(row) {
return row > 1 && row < 100;
}
function getUserDetails(sheet, row) {
const idCol = findColumnIndex(sheet, "id");
const forenamesCol = findColumnIndex(sheet, "Forenames");
return {
userId: sheet.getRange(row, idCol).getValue(),
firstName: sheet.getRange(row, forenamesCol).getValue(),
};
}
function confirmAction(ui, firstName, userId) {
const response = ui.alert(
"Confirm Update",
`Update membership status for ${firstName}?\nUser ID: ${userId}`,
ui.ButtonSet.OK_CANCEL,
);
return response === ui.Button.OK;
}
function updateWordPressMembership(userId, metaKey, metaValue) {
const { user, datetime } = getCurrentUserAndTime();
const data = {
meta_data: [
{ key: metaKey, value: metaValue },
{ key: `${metaKey}_marked_given_at`, value: datetime },
{ key: `${metaKey}_marked_given_by`, value: user },
],
};
const response = pokeToWooUserMeta(data, userId);
const userData = JSON.parse(response.getContentText());
return userData.meta_data.some(
(meta) => meta.key === metaKey && meta.value === metaValue,
);
}
function updateSheetStatus(sheet, row) {
const statusCol = findColumnIndex(sheet, "Status");
if (statusCol) {
sheet.getRange(row, statusCol).setValue("Given");
}
}
// Wrapper functions for menu items
function unMembership() {
updateMembershipStatus("cc_member", "no");
}
function giveMembership() {
updateMembershipStatus("cc_member", "yes");
}