import { Client, Users, Databases, Query } from 'node-appwrite';
export default async ({ req, res, log, error }) => {
const client = new Client()
.setEndpoint(process.env.ENDPOINT)
.setProject(process.env.PROJECT_ID)
.setKey(req.headers['x-appwrite-key']);
const users = new Users(client);
const databases = new Databases(client);
try {
if (!req.body) throw new Error('Missing request body');
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
const userId = data?.$id;
log('userId:', userId);
if (!userId) {
throw new Error('Missing user ID in request')
};
const user = await users.get(userId);
log('user:', user);
const profileId = user.prefs?.profile_id;
log('profileId:', profileId);
if (profileId) {
await Promise.all([
databases.deleteDocuments(
process.env.DATABASE_ID,
process.env.SAVES_COLLECTION,
[Query.equal('userId', profileId)]
),
databases.deleteDocument(
process.env.DATABASE_ID,
process.env.USERNAMES_COLLECTION,
profileId
)
]);
}
await users.delete(userId);
return res.json({ success: true, deletedProfileId: profileId });
} catch (err) {
error('Failed to delete user: ' + err.message);
return res.json({ success: false, error: err.message });
}
};