birthday through qr, deleteFam function

This commit is contained in:
ivic00
2025-02-12 00:05:39 +01:00
parent 6ada9470c3
commit a8957c7ac7
6 changed files with 247 additions and 196 deletions

View File

@ -1771,3 +1771,83 @@ exports.updateHouseholdTimestampOnEventUpdate = functions.firestore
throw error;
}
});
exports.deleteFamily = functions.https.onCall(async (data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('unauthenticated', 'User must be authenticated');
}
const { familyId } = data;
if (!familyId) {
throw new functions.https.HttpsError('invalid-argument', 'Family ID is required');
}
try {
const db = admin.firestore();
const requestingUserProfile = await db.collection('Profiles')
.doc(context.auth.uid)
.get();
if (!requestingUserProfile.exists || requestingUserProfile.data().userType !== 'parent') {
throw new functions.https.HttpsError('permission-denied', 'Only parents can delete families');
}
if (requestingUserProfile.data().familyId !== familyId) {
throw new functions.https.HttpsError('permission-denied', 'You can not delete other families');
}
const profilesSnapshot = await db.collection('Profiles')
.where('familyId', '==', familyId)
.get();
const batch = db.batch();
const profileIds = [];
for (const profile of profilesSnapshot.docs) {
const userId = profile.id;
profileIds.push(userId);
const collections = [
'BrainDumps',
'Groceries',
'Todos',
'Events'
];
for (const collectionName of collections) {
const userDocsSnapshot = await db.collection(collectionName)
.where('creatorId', '==', userId)
.get();
userDocsSnapshot.docs.forEach(doc => {
batch.delete(doc.ref);
});
}
batch.delete(profile.ref);
}
const householdDoc = await db.collection('Households')
.doc(familyId)
.get();
if (householdDoc.exists) {
batch.delete(householdDoc.ref);
}
await batch.commit();
// Delete Firebase Auth accounts
await Promise.all(profileIds.map(userId =>
admin.auth().deleteUser(userId)
));
return { success: true, message: 'Family deleted successfully' };
} catch (error) {
console.error('Error deleting family:', error);
throw new functions.https.HttpsError('internal', 'Error deleting family data');
}
});