Problem to deploy

Hello,

I can’t do a “npm run build” I get an error message.

Can you help me ?

You get many error messages. Which one are you having trouble fixing?

I don’t see what to do in all the error messages I don’t really understand what to do

So you read:
actions.ts 58:16 Error: 'newInvoice' is assigned a value but never used.

and dont know what to do?

Your screenshot has 7 errors in it. Start at the top, work your way down, fix each one.

2 Likes

I will try to do that, thank you, I will get back to you in case I can’t find it.

the error messages are giving you the file name, the line number, and the character in that line where the error was raised (./app/actions.ts, line 58, character 16, for example, is the first one) so you can definitely find them.

1 Like

Thank you, I understood the error message, for example for this one where “newInvoice” is not used, the problem is that I don’t see how to use it.

code actions.ts

"use server"

import prisma from "@/lib/prisma";
import { Invoice } from "@/type";
import { randomBytes } from "crypto";

export async function checkAndAddUser(email: string, name: string) {
    if (!email) return;
    try {
        const existingUser = await prisma.user.findUnique({
            where: {
                email: email
            }
        })

        if (!existingUser && name) {
            await prisma.user.create({
                data: {
                    email,
                    name
                }
            })
        }

    } catch (error) {
        console.error(error)
    }
}

const generateUniqueId = async () => {
    let uniqueId;
    let isUnique = false;

    while (!isUnique) {
        uniqueId = randomBytes(3).toString('hex')
        const existingInvoice = await prisma.invoice.findUnique({
            where: {
                id: uniqueId
            }
        })
        if (!existingInvoice) {
            isUnique = true;
        }
    }
    return uniqueId
}

export async function createEmptyInvoice(email: string, name: string) {
    try {
        const user = await prisma.user.findUnique({
            where: {
                email: email
            }
        })

        const invoiceId = await generateUniqueId() as string

        if (user) {
            const newInvoice = await prisma.invoice.create({
                data: {
                    id: invoiceId,
                    name: name,
                    userId: user?.id,
                    issuerName: "",
                    issuerAddress: "",
                    clientName: "",
                    clientAddress: "",
                    invoiceDate: "",
                    dueDate: "",
                    vatActive: false,
                    vatRate: 20,
                }
            })
        }
    } catch (error) {
        console.error(error)
    }
}

export async function getInvoicesByEmail(email: string) {
    if (!email) return;
    try {
        const user = await prisma.user.findUnique({
            where: {
                email: email
            },
            include: {
                invoices: {
                    include: {
                        lines: true,
                    }
                }
            }
        })
        // Statuts possibles :
        // 1: Brouillon
        // 2: En attente
        // 3: Payée
        // 4: Annulée
        // 5: Impayé
        if (user) {
            const today = new Date()
            const updatedInvoices = await Promise.all(
                user.invoices.map(async (invoice) => {
                    const dueDate = new Date(invoice.dueDate)
                    if (
                        dueDate < today &&
                        invoice.status == 2
                    ) {
                        const updatedInvoice = await prisma.invoice.update({
                            where: { id: invoice.id },
                            data: { status: 5 },
                            include: { lines: true }
                        })
                        return updatedInvoice
                    }
                    return invoice
                })
            )
            return updatedInvoices

        }
    } catch (error) {
        console.error(error)
    }
}

export async function getInvoiceById(invoiceId: string) {
    try {
        const invoice = await prisma.invoice.findUnique({
            where: { id: invoiceId },
            include: {
                lines: true
            }
        })
        if (!invoice) {
            throw new Error("Facture non trouvée.");
        }
        return invoice
    } catch (error) {
        console.error(error)
    }
}

export async function updateInvoice(invoice: Invoice) {
    try {
        const existingInvoice = await prisma.invoice.findUnique({
            where: { id: invoice.id },
            include: {
                lines: true
            }
        })

        if (!existingInvoice) {
            throw new Error(`Facture avec l'ID ${invoice.id} introuvable.`);
        }

        await prisma.invoice.update({
            where: { id: invoice.id },
            data: {
                issuerName: invoice.issuerName,
                issuerAddress: invoice.issuerAddress,
                clientName: invoice.clientName,
                clientAddress: invoice.clientAddress,
                invoiceDate: invoice.invoiceDate,
                dueDate: invoice.dueDate,
                vatActive: invoice.vatActive,
                vatRate: invoice.vatRate,
                status: invoice.status,
            },
        })

        const existingLines = existingInvoice.lines

        const receivedLines = invoice.lines

        const linesToDelete = existingLines.filter(
            (existingLine) => !receivedLines.some((line) => line.id === existingLine.id)
        )

        if (linesToDelete.length > 0) {
            await prisma.invoiceLine.deleteMany({
                where: {
                    id: { in: linesToDelete.map((line) => line.id) }
                }
            })
        }

        for (const line of receivedLines) {
            const existingLine = existingLines.find((l) => l.id == line.id)
            if (existingLine) {
                const hasChanged =
                    line.description !== existingLine.description ||
                    line.quantity !== existingLine.quantity ||
                    line.unitPrice !== existingLine.unitPrice;

                if (hasChanged) {
                    await prisma.invoiceLine.update({
                        where: { id: line.id },
                        data: {
                            description: line.description,
                            quantity: line.quantity,
                            unitPrice: line.unitPrice,

                        }
                    })
                }
            } else {
                //créer une nouvelle ligne
                await prisma.invoiceLine.create({
                    data: {
                        description: line.description,
                        quantity: line.quantity,
                        unitPrice: line.unitPrice,
                        invoiceId: invoice.id
                    }
                })

            }
        }

    } catch (error) {
        console.error(error)
    }
}

export async function deleteInvoice(invoiceId: string) {
    try {
        const deleteInvoice = await prisma.invoice.delete({
            where: { id: invoiceId }
        })
        if (!deleteInvoice) {
            throw new Error("Erreur lors de la suppression de la facture.");
        }
    } catch (error) {
        console.error(error)
    }
}

Well if you dont know how to use it, why did you define it?

1 Like

I followed the tutorial that I finished and I wanted to put it online
https://www.youtube.com/watch?v=VoKDkMjE9LU

So then either:

  • You copied a bad tutorial because it doesnt compile,
  • You didnt copy it correctly or completely
  • You didnt finish the tutorial series.

Like, i didnt write the tutorial. So if you want a Why does this variable exist… go ask the person who wrote it.

I’m not going to extrapolate a reason for someone else’s code. The error says the variable isnt used. So either use it, or remove it.

1 Like

The author of the code seems to have made a mistake. It is not used anywhere. So just remove it.

The rest of the functions are being used here:

https://github.com/sadikou-faiz/invoice/blob/main/app/invoice/%5BinvoiceId%5D/page.tsx

import { deleteInvoice, getInvoiceById, updateInvoice } from ‘@/app/actions’

Also use the same compiler options the author is using:

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.