mirror of
https://github.com/urosran/cally.git
synced 2025-07-10 15:17:17 +00:00
birthday through qr, deleteFam function
This commit is contained in:
@ -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');
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user