N-FIN-61: fix prisma client instantiation

This commit is contained in:
Markus Thielker 2024-03-24 16:16:02 +01:00
parent 7731d0143e
commit 00b4e51aee
No known key found for this signature in database
17 changed files with 82 additions and 66 deletions

View file

@ -1,7 +1,7 @@
import { ActionResponse } from '@/lib/types/actionResponse';
import { URL_SIGN_IN } from '@/lib/constants';
import { getUser, lucia } from '@/auth';
import { prismaClient } from '@/prisma';
import prisma from '@/prisma';
import { cookies } from 'next/headers';
export default async function accountDelete(): Promise<ActionResponse> {
@ -17,31 +17,31 @@ export default async function accountDelete(): Promise<ActionResponse> {
};
}
await prismaClient.payment.deleteMany({
await prisma.payment.deleteMany({
where: {
userId: user.id,
},
});
await prismaClient.entity.deleteMany({
await prisma.entity.deleteMany({
where: {
userId: user.id,
},
});
await prismaClient.category.deleteMany({
await prisma.category.deleteMany({
where: {
userId: user.id,
},
});
await prismaClient.session.deleteMany({
await prisma.session.deleteMany({
where: {
userId: user.id,
},
});
await prismaClient.user.delete({
await prisma.user.delete({
where: {
id: user.id,
},

View file

@ -1,6 +1,6 @@
import { z } from 'zod';
import { ActionResponse } from '@/lib/types/actionResponse';
import { prismaClient } from '@/prisma';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
import { categoryFormSchema } from '@/lib/form-schemas/categoryFormSchema';
@ -25,7 +25,7 @@ export default async function categoryCreateUpdate({
// create/update category
try {
if (id) {
await prismaClient.category.update({
await prisma.category.update({
where: {
id: id,
},
@ -42,7 +42,7 @@ export default async function categoryCreateUpdate({
message: `'${name}' updated`,
};
} else {
await prismaClient.category.create({
await prisma.category.create({
data: {
userId: user.id,
name: name,

View file

@ -1,5 +1,5 @@
import { ActionResponse } from '@/lib/types/actionResponse';
import { prismaClient } from '@/prisma';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
@ -25,7 +25,7 @@ export default async function categoryDelete(id: number): Promise<ActionResponse
}
// check that category is associated with user
const category = await prismaClient.category.findFirst({
const category = await prisma.category.findFirst({
where: {
id: id,
userId: user.id,
@ -40,7 +40,7 @@ export default async function categoryDelete(id: number): Promise<ActionResponse
// delete category
try {
await prismaClient.category.delete({
await prisma.category.delete({
where: {
id: category.id,
userId: user.id,

View file

@ -1,7 +1,7 @@
import { z } from 'zod';
import { ActionResponse } from '@/lib/types/actionResponse';
import { entityFormSchema } from '@/lib/form-schemas/entityFormSchema';
import { prismaClient } from '@/prisma';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
@ -26,7 +26,7 @@ export default async function entityCreateUpdate({
// create/update entity
try {
if (id) {
await prismaClient.entity.update({
await prisma.entity.update({
where: {
id: id,
},
@ -44,7 +44,7 @@ export default async function entityCreateUpdate({
message: `${type} '${name}' updated`,
};
} else {
await prismaClient.entity.create({
await prisma.entity.create({
data: {
userId: user.id,
name: name,

View file

@ -1,5 +1,5 @@
import { ActionResponse } from '@/lib/types/actionResponse';
import { prismaClient } from '@/prisma';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
@ -25,7 +25,7 @@ export default async function entityDelete(id: number): Promise<ActionResponse>
}
// check that entity is associated with user
const entity = await prismaClient.entity.findFirst({
const entity = await prisma.entity.findFirst({
where: {
id: id,
userId: user.id,
@ -40,7 +40,7 @@ export default async function entityDelete(id: number): Promise<ActionResponse>
// delete entity
try {
await prismaClient.entity.delete({
await prisma.entity.delete({
where: {
id: entity.id,
userId: user.id,

View file

@ -1,4 +1,4 @@
import { prismaClient } from '@/prisma';
import prisma from '@/prisma';
import type { Category, Entity } from '@prisma/client';
import { EntityType } from '@prisma/client';
import { getUser } from '@/auth';
@ -19,12 +19,12 @@ export default async function generateSampleData(): Promise<ActionResponse> {
}
// Categories: create sample data
const categories: Category[] = await prismaClient.category.findMany({where: {userId: user.id}});
if (await prismaClient.category.count({where: {userId: user.id}}) == 0) {
const categories: Category[] = await prisma.category.findMany({where: {userId: user.id}});
if (await prisma.category.count({where: {userId: user.id}}) == 0) {
console.log('Creating sample categories...');
categories.push(await prismaClient.category.create({
categories.push(await prisma.category.create({
data: {
userId: user.id,
name: 'Groceries',
@ -32,7 +32,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
},
}));
categories.push(await prismaClient.category.create({
categories.push(await prisma.category.create({
data: {
userId: user.id,
name: 'Drugstore items',
@ -40,7 +40,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
},
}));
categories.push(await prismaClient.category.create({
categories.push(await prisma.category.create({
data: {
userId: user.id,
name: 'Going out',
@ -48,7 +48,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
},
}));
categories.push(await prismaClient.category.create({
categories.push(await prisma.category.create({
data: {
userId: user.id,
name: 'Random stuff',
@ -56,7 +56,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
},
}));
categories.push(await prismaClient.category.create({
categories.push(await prisma.category.create({
data: {
userId: user.id,
name: 'Salary',
@ -69,12 +69,12 @@ export default async function generateSampleData(): Promise<ActionResponse> {
console.log(categories);
// Entities: create sample data
const entities: Entity[] = await prismaClient.entity.findMany({where: {userId: user.id}});
if (await prismaClient.entity.count({where: {userId: user.id}}) == 0) {
const entities: Entity[] = await prisma.entity.findMany({where: {userId: user.id}});
if (await prisma.entity.count({where: {userId: user.id}}) == 0) {
console.log('Creating sample entities...');
entities.push(await prismaClient.entity.create({
entities.push(await prisma.entity.create({
data: {
userId: user.id,
name: 'Main Account',
@ -82,7 +82,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
},
}));
entities.push(await prismaClient.entity.create({
entities.push(await prisma.entity.create({
data: {
userId: user.id,
name: 'Company',
@ -90,7 +90,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
},
}));
entities.push(await prismaClient.entity.create({
entities.push(await prisma.entity.create({
data: {
userId: user.id,
name: 'Supermarket 1',
@ -98,7 +98,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
},
}));
entities.push(await prismaClient.entity.create({
entities.push(await prisma.entity.create({
data: {
userId: user.id,
name: 'Supermarket 2',
@ -106,7 +106,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
},
}));
entities.push(await prismaClient.entity.create({
entities.push(await prisma.entity.create({
data: {
userId: user.id,
name: 'Supermarket 3',
@ -114,7 +114,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
},
}));
entities.push(await prismaClient.entity.create({
entities.push(await prisma.entity.create({
data: {
userId: user.id,
name: 'Supermarket 4',
@ -129,14 +129,14 @@ export default async function generateSampleData(): Promise<ActionResponse> {
// Payments: create sample data
console.log('Creating sample payments...');
if (await prismaClient.payment.count({where: {userId: user.id}}) == 0) {
if (await prisma.payment.count({where: {userId: user.id}}) == 0) {
for (let i = 0; i < 4; i++) {
const date = new Date();
date.setDate(1);
date.setMonth(date.getMonth() - i);
await prismaClient.payment.create({
await prisma.payment.create({
data: {
userId: user.id,
amount: 200000,
@ -164,7 +164,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
const date = new Date(
new Date().getTime() - Math.floor(Math.random() * 10000000000));
await prismaClient.payment.create({
await prisma.payment.create({
data: {
userId: user.id,
amount: Math.floor(

View file

@ -1,6 +1,6 @@
import { z } from 'zod';
import { ActionResponse } from '@/lib/types/actionResponse';
import { prismaClient } from '@/prisma';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
import { paymentFormSchema } from '@/lib/form-schemas/paymentFormSchema';
@ -29,7 +29,7 @@ export default async function paymentCreateUpdate({
// create/update payment
try {
if (id) {
await prismaClient.payment.update({
await prisma.payment.update({
where: {
id: id,
},
@ -50,7 +50,7 @@ export default async function paymentCreateUpdate({
message: `Payment updated`,
};
} else {
await prismaClient.payment.create({
await prisma.payment.create({
data: {
userId: user.id,
amount: amount,

View file

@ -1,5 +1,5 @@
import { ActionResponse } from '@/lib/types/actionResponse';
import { prismaClient } from '@/prisma';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
@ -25,7 +25,7 @@ export default async function paymentDelete(id: number): Promise<ActionResponse>
}
// check that payment is associated with user
const payment = await prismaClient.payment.findFirst({
const payment = await prisma.payment.findFirst({
where: {
id: id,
userId: user.id,
@ -40,7 +40,7 @@ export default async function paymentDelete(id: number): Promise<ActionResponse>
// delete payment
try {
await prismaClient.payment.delete({
await prisma.payment.delete({
where: {
id: payment.id,
userId: user.id,

View file

@ -5,12 +5,12 @@ import { cookies } from 'next/headers';
import { signInFormSchema } from '@/lib/form-schemas/signInFormSchema';
import { ActionResponse } from '@/lib/types/actionResponse';
import { URL_HOME } from '@/lib/constants';
import { prismaClient } from '@/prisma';
import prisma from '@/prisma';
export default async function signIn({username, password}: z.infer<typeof signInFormSchema>): Promise<ActionResponse> {
'use server';
const existingUser = await prismaClient.user.findFirst({
const existingUser = await prisma.user.findFirst({
where: {
username: username.toLowerCase(),
},

View file

@ -6,7 +6,7 @@ import { cookies } from 'next/headers';
import { signUpFormSchema } from '@/lib/form-schemas/signUpFormSchema';
import { ActionResponse } from '@/lib/types/actionResponse';
import { URL_HOME } from '@/lib/constants';
import { prismaClient } from '@/prisma';
import prisma from '@/prisma';
export default async function signUp({username, password}: z.infer<typeof signUpFormSchema>): Promise<ActionResponse> {
'use server';
@ -14,7 +14,7 @@ export default async function signUp({username, password}: z.infer<typeof signUp
const hashedPassword = await new Argon2id().hash(password);
const userId = generateId(15);
const existingUser = await prismaClient.user.findFirst({
const existingUser = await prisma.user.findFirst({
where: {
username: username.toLowerCase(),
},
@ -27,7 +27,7 @@ export default async function signUp({username, password}: z.infer<typeof signUp
};
}
await prismaClient.user.create({
await prisma.user.create({
data: {
id: userId,
username: username,