Manqo
Manqo
PPrisma
Created by Manqo on 10/9/2024 in #help-and-questions
I’m trying to fix an error I’m encountering in my Prisma code: This expression is not callable.
Actually the code i provided does not work correctly with the sortOrder, but this one works:
export async function createExperience(
data: Partial<UnifiedFormType> & { formType: FormType; resumeId: string },
) {
const { formType, ...formData } = data;
let createdItem;
const getHighestSortOrder = async (model: any) => {
const highestItem = await model.findFirst({
where: { resumeId: data.resumeId },
orderBy: { sortOrder: 'desc' },
});
return highestItem?.sortOrder ?? -1; //
};

const assignNextSortOrder = async (model: any) => {
const highestSortOrder = await getHighestSortOrder(model);
return highestSortOrder + 1;
};
switch (formType) {
case 'workexperiences':
createdItem = await db.workExperience.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.workExperience),
},
});
break;
case 'educations':
createdItem = await db.education.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.education),
},
});
break;
case 'languages':
createdItem = await db.language.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.language),
},
});
break;
case 'licences':
createdItem = await db.licence.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.licence),
},
});
break;
default:
throw new Error('Invalid form type');
}

revalidatePath(`/resume/${data.resumeId}`);
return createdItem;
}
export async function createExperience(
data: Partial<UnifiedFormType> & { formType: FormType; resumeId: string },
) {
const { formType, ...formData } = data;
let createdItem;
const getHighestSortOrder = async (model: any) => {
const highestItem = await model.findFirst({
where: { resumeId: data.resumeId },
orderBy: { sortOrder: 'desc' },
});
return highestItem?.sortOrder ?? -1; //
};

const assignNextSortOrder = async (model: any) => {
const highestSortOrder = await getHighestSortOrder(model);
return highestSortOrder + 1;
};
switch (formType) {
case 'workexperiences':
createdItem = await db.workExperience.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.workExperience),
},
});
break;
case 'educations':
createdItem = await db.education.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.education),
},
});
break;
case 'languages':
createdItem = await db.language.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.language),
},
});
break;
case 'licences':
createdItem = await db.licence.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.licence),
},
});
break;
default:
throw new Error('Invalid form type');
}

revalidatePath(`/resume/${data.resumeId}`);
return createdItem;
}
But as u can this implementation is not so clean. But anyway i get still the same problem, i dont know what type i should put to the any.
const getHighestSortOrder = async (model: any) => {


const assignNextSortOrder = async (model: any) => {
const getHighestSortOrder = async (model: any) => {


const assignNextSortOrder = async (model: any) => {
So what type i should put here instead of any?
3 replies
PPrisma
Created by Manqo on 9/25/2024 in #help-and-questions
Deleting User Account and All Related Data
Yeah i think i got The code working. The problem was with The nextauth, it tried to signout The user after The deletion.
11 replies
PPrisma
Created by Manqo on 9/25/2024 in #help-and-questions
Deleting User Account and All Related Data
Okey so basically it should be enought that i call
await db.$transaction([
db.user.delete({ where: { id: userId } }),
]);
return { success: true };
await db.$transaction([
db.user.delete({ where: { id: userId } }),
]);
return { success: true };
11 replies
PPrisma
Created by Manqo on 9/25/2024 in #help-and-questions
Deleting User Account and All Related Data
Thanks, so basically i sohuld be adding onDelete:cascade to all models that are related to user?
11 replies
PPrisma
Created by Manqo on 9/25/2024 in #help-and-questions
Deleting User Account and All Related Data
What should I do to get rid of this error? The deletion actually works, and it redirects the user to the main page afterward, but having an error like this doesn't seem right. The problem is that I'm getting an error that says:
PrismaClientKnownRequestError: Invalid `prisma.session.delete()` invocation: An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.
at Cn.handleRequestError (.../library.js:123:6817)
at Cn.request (.../library.js:123:5926)
at async l (.../library.js:128:9968)
{
name: 'DeleteSessionError',
code: 'P2025'
}
PrismaClientKnownRequestError: Invalid `prisma.session.delete()` invocation: An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.
at Cn.handleRequestError (.../library.js:123:6817)
at Cn.request (.../library.js:123:5926)
at async l (.../library.js:128:9968)
{
name: 'DeleteSessionError',
code: 'P2025'
}
Here is part of my prisma schema.
model User {
id String @id @default(cuid())
name String
email String @unique
emailVerified DateTime?
image String?

accounts Account[]
sessions Session[]
resumes Resume[]

stripeCustomerId String? @unique @map(name: "stripe_customer_id")
stripeSubscriptionId String? @unique @map(name: "stripe_subscription_id")
stripePriceId String? @map(name: "stripe_price_id")
stripeCurrentPeriodEnd DateTime? @map(name: "stripe_current_period_end")

coverLetterGenerations Int @default(2)
Coverletter Coverletter[]
}

model Resume {
id String @id @default(cuid())
userId String
user User @relation(fields: [userId], references: [id])
title String
createdAt DateTime @default(now()) @map(name: "created_at")
updatedAt DateTime @updatedAt @map(name: "updated_at")
personalInfo PersonalInfo?
workexperiences WorkExperience[]
educations Education[]
...
fontFamily String @default("Helvetica")
templateStyle String @default("Example1")

workExperienceTitle String @default("Työkokemus")
...
}
model User {
id String @id @default(cuid())
name String
email String @unique
emailVerified DateTime?
image String?

accounts Account[]
sessions Session[]
resumes Resume[]

stripeCustomerId String? @unique @map(name: "stripe_customer_id")
stripeSubscriptionId String? @unique @map(name: "stripe_subscription_id")
stripePriceId String? @map(name: "stripe_price_id")
stripeCurrentPeriodEnd DateTime? @map(name: "stripe_current_period_end")

coverLetterGenerations Int @default(2)
Coverletter Coverletter[]
}

model Resume {
id String @id @default(cuid())
userId String
user User @relation(fields: [userId], references: [id])
title String
createdAt DateTime @default(now()) @map(name: "created_at")
updatedAt DateTime @updatedAt @map(name: "updated_at")
personalInfo PersonalInfo?
workexperiences WorkExperience[]
educations Education[]
...
fontFamily String @default("Helvetica")
templateStyle String @default("Example1")

workExperienceTitle String @default("Työkokemus")
...
}
11 replies