N-FIN-79: refactor account data reset

This commit is contained in:
Markus Thielker 2024-12-23 00:06:09 +01:00
parent 98f29a8366
commit f378e2a045
No known key found for this signature in database
4 changed files with 54 additions and 62 deletions

View file

@ -1,52 +0,0 @@
import { ActionResponse } from '@/lib/types/actionResponse';
import { URL_SIGN_IN, URL_SIGN_OUT } from '@/lib/constants';
import prisma from '@/prisma';
import { getSession } from '@auth0/nextjs-auth0';
export default async function accountDelete(): Promise<ActionResponse> {
'use server';
const session = await getSession();
if (!session) {
return {
type: 'error',
message: 'You aren\'t signed in.',
redirect: URL_SIGN_IN,
};
}
const user = session.user;
await prisma.payment.deleteMany({
where: {
userId: user.sub,
},
});
await prisma.entity.deleteMany({
where: {
userId: user.sub,
},
});
await prisma.category.deleteMany({
where: {
userId: user.sub,
},
});
let requestOptions = {
method: 'DELETE',
redirect: 'follow',
} as RequestInit;
fetch(`https://login.auth0.com/api/v2/users/${user.sub}`, requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
return {
type: 'success',
message: 'Your account was removed.',
redirect: URL_SIGN_OUT,
};
}

View file

@ -0,0 +1,40 @@
import { ActionResponse } from '@/lib/types/actionResponse';
import { URL_SIGN_IN } from '@/lib/constants';
import prisma from '@/prisma';
import { getSession } from '@auth0/nextjs-auth0';
export default async function clearAccountData(): Promise<ActionResponse> {
'use server';
const session = await getSession();
if (!session) {
return {
type: 'error',
message: 'You aren\'t signed in.',
redirect: URL_SIGN_IN,
};
}
await prisma.payment.deleteMany({
where: {
userId: session.user.sub,
},
});
await prisma.entity.deleteMany({
where: {
userId: session.user.sub,
},
});
await prisma.category.deleteMany({
where: {
userId: session.user.sub,
},
});
return {
type: 'success',
message: 'Your account data was cleared.',
};
}