-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpostProfile.js
More file actions
66 lines (55 loc) · 1.49 KB
/
postProfile.js
File metadata and controls
66 lines (55 loc) · 1.49 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
import { https, logger } from 'firebase-functions/v2';
import { getFirestore } from 'firebase-admin/firestore';
import { profileSchema } from '../shared/cornerSubmission/Schema.js';
import { safelyInitializeApp } from '../firebase.js';
safelyInitializeApp();
const db = getFirestore();
const options = {
stripUnknown: true,
abortEarly: false,
};
export const updateProfile = async (data, uid) => {
logger.info('validating profile data', data, uid, {
structuredData: true,
});
try {
const result = await validate(data);
logger.debug('validation result', result, {
structuredData: true,
});
} catch (error) {
logger.error('validation error', error, {
structuredData: true,
});
throw new https.HttpsError(
'invalid-argument',
'form data is invalid',
error,
);
}
const doc = formatDataForFirestore(data);
logger.info('saving profile', doc, uid, {
structuredData: true,
});
try {
const docRef = db.collection('submitters').doc(uid);
await docRef.update(doc);
} catch (error) {
logger.error('error saving profile', error, doc, {
structuredData: true,
});
throw new https.HttpsError('internal', 'The profile was not saved');
}
return doc;
};
export const validate = async (data) => {
await profileSchema.validate(data, options);
};
const formatDataForFirestore = (data) => {
return {
displayName: data.displayName,
email: data.email,
license: data?.license,
seal: data?.seal,
};
};