-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.gs
More file actions
128 lines (111 loc) · 3.92 KB
/
edit.gs
File metadata and controls
128 lines (111 loc) · 3.92 KB
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
function doGet(e) {
try {
const { action, token, email, data } = e.parameter;
// Session verification endpoint
if (action === 'verify_session') {
const session = verifySession(token);
return ContentService.createTextOutput(JSON.stringify({
valid: !!session,
email: session?.email,
expiry: session?.expiry
})).setMimeType(ContentService.MimeType.JSON);
}
// Profile update endpoint
if (action === 'update_profile') {
const session = verifySession(token);
if (!session || session.email.toLowerCase() !== email.toLowerCase()) {
throw new Error('SESSION_EXPIRED');
}
let parsedData;
try {
parsedData = JSON.parse(decodeURIComponent(data));
} catch (error) {
throw new Error('INVALID_DATA_FORMAT');
}
const result = handleProfileUpdate(email, parsedData);
return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON);
}
throw new Error('INVALID_ACTION');
} catch (error) {
return ContentService.createTextOutput(JSON.stringify({
status: 'error',
message: error.message
})).setMimeType(ContentService.MimeType.JSON);
}
}
function verifySession(token) {
if (!token) return null;
const cacheKey = `session_${token}`;
const cached = CacheService.getScriptCache().get(cacheKey);
if (!cached) return null;
try {
const session = JSON.parse(cached);
if (Date.now() > session.expiry) {
CacheService.getScriptCache().remove(cacheKey);
return null;
}
return session;
} catch (e) {
return null;
}
}
function handleProfileUpdate(email, updateData) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form');
const data = sheet.getDataRange().getValues();
const headers = data[0].map(h => h.toString().trim().toLowerCase());
const COLUMNS = {
EMAIL: headers.indexOf('email'),
NAME: headers.indexOf('name'),
TAGLINE: headers.indexOf('tagline'),
PHONE: headers.indexOf('phone'),
ADDRESS: headers.indexOf('address'),
SOCIAL_LINKS: headers.indexOf('social links'),
PROFILE_PIC: headers.indexOf('profile picture') !== -1 ?
headers.indexOf('profile picture') :
headers.indexOf('profile pic'),
TIMESTAMP: headers.indexOf('timestamp')
};
const normalizedEmail = email.trim().toLowerCase();
const rowIndex = data.findIndex((row, idx) =>
idx > 0 && row[COLUMNS.EMAIL] &&
row[COLUMNS.EMAIL].toString().trim().toLowerCase() === normalizedEmail
);
if (rowIndex === -1) {
throw new Error('PROFILE_NOT_FOUND');
}
const row = rowIndex + 1;
const updates = {};
if (updateData.name !== undefined && COLUMNS.NAME !== -1) {
updates[COLUMNS.NAME] = updateData.name;
}
if (updateData.tagline !== undefined && COLUMNS.TAGLINE !== -1) {
updates[COLUMNS.TAGLINE] = updateData.tagline;
}
if (updateData.phone !== undefined && COLUMNS.PHONE !== -1) {
updates[COLUMNS.PHONE] = updateData.phone;
}
if (updateData.address !== undefined && COLUMNS.ADDRESS !== -1) {
updates[COLUMNS.ADDRESS] = updateData.address;
}
if (updateData.profilePic !== undefined && COLUMNS.PROFILE_PIC !== -1) {
updates[COLUMNS.PROFILE_PIC] = updateData.profilePic;
}
if (updateData.socialLinks !== undefined && COLUMNS.SOCIAL_LINKS !== -1) {
updates[COLUMNS.SOCIAL_LINKS] = updateData.socialLinks.join('\n');
}
if (COLUMNS.TIMESTAMP !== -1) {
updates[COLUMNS.TIMESTAMP] = new Date().toISOString();
}
const rowData = sheet.getRange(row, 1, 1, headers.length).getValues()[0];
Object.entries(updates).forEach(([col, value]) => {
rowData[col] = value;
});
sheet.getRange(row, 1, 1, headers.length).setValues([rowData]);
return {
status: 'success',
message: 'Profile updated successfully',
timestamp: updates[COLUMNS.TIMESTAMP],
updatedFields: Object.keys(updateData)
};
}