Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: import of csv and make github field optional #49

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CREATE TABLE "User" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"github" TEXT NOT NULL,
"github" TEXT NOT NULL DEFAULT 'ghost',
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
Expand All @@ -29,7 +29,7 @@ CREATE TABLE "Play" (
"updatedAt" DATETIME NOT NULL,

PRIMARY KEY ("userId", "eventSlug"),
CONSTRAINT "Play_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "Play_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "Play_eventSlug_fkey" FOREIGN KEY ("eventSlug") REFERENCES "Event" ("slug") ON DELETE RESTRICT ON UPDATE CASCADE
);

Expand All @@ -41,6 +41,3 @@ CREATE UNIQUE INDEX "Event_slug_key" ON "Event"("slug");

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE UNIQUE INDEX "User_github_key" ON "User"("github");
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ model User {
id String @id @default(uuid())
name String
email String @unique
github String @unique
github String @default("ghost")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
play Play[]
Expand All @@ -38,7 +38,7 @@ model Play {
winner Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
event Event @relation(fields: [eventSlug], references: [slug])

@@id([userId, eventSlug])
Expand Down
10 changes: 8 additions & 2 deletions prisma/seed/json-to-csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ import { writeFile } from "node:fs/promises"
import { participants } from "./participants"

async function main() {
let content = "Nome;Email;GitHub\n"
const title =
"N. do pedido,Data do pedido,Status do participante,Nome,Sobrenome,E-mail\n"
let content = title
content += participants
.map(({ name, email, github }) => `${name};${email};${github}`)
.map(({ name, email }) => {
const [firstName, ...lastNameArray] = name.split(" ")
const lastName = lastNameArray.join(" ")
return `0,0/0/00 10:40 AM,0,${firstName},${lastName},${email}`
})
.join("\n")

await writeFile("./frontin.csv", content)
Expand Down
40 changes: 0 additions & 40 deletions prisma/seed/participants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,201 +4,161 @@ export const participants: RegisterParticipant[] = [
{
name: "Eal Nottle",
email: "[email protected]",
github: "enottle0",
},
{
name: "Sibyl Spence",
email: "[email protected]",
github: "sspence1",
},
{
name: "Shari Coan",
email: "[email protected]",
github: "scoan2",
},
{
name: "Sheena Bend",
email: "[email protected]",
github: "sbend3",
},
{
name: "Bartlet Boote",
email: "[email protected]",
github: "bboote4",
},
{
name: "Jourdan Kemson",
email: "[email protected]",
github: "jkemson5",
},
{
name: "Rudy McGoon",
email: "[email protected]",
github: "rmcgoon6",
},
{
name: "Katalin Grief",
email: "[email protected]",
github: "kgrief7",
},
{
name: "Tuckie Hartzenberg",
email: "[email protected]",
github: "thartzenberg8",
},
{
name: "Gabby Sivior",
email: "[email protected]",
github: "gsivior9",
},
{
name: "Bliss Madgwich",
email: "[email protected]",
github: "bmadgwicha",
},
{
name: "Shaylah Wetherell",
email: "[email protected]",
github: "swetherellb",
},
{
name: "Barnie Chsteney",
email: "[email protected]",
github: "bchsteneyc",
},
{
name: "Orlan Beamond",
email: "[email protected]",
github: "obeamondd",
},
{
name: "Jacinta McKevin",
email: "[email protected]",
github: "jmckevine",
},
{
name: "Hasheem Teers",
email: "[email protected]",
github: "hteersf",
},
{
name: "Wernher Grim",
email: "[email protected]",
github: "wgrimg",
},
{
name: "Nelia Tambling",
email: "[email protected]",
github: "ntamblingh",
},
{
name: "Tore Cumberlidge",
email: "[email protected]",
github: "tcumberlidgei",
},
{
name: "Carmelle Padson",
email: "[email protected]",
github: "cpadsonj",
},
{
name: "Prent Stannett",
email: "[email protected]",
github: "pstannettk",
},
{
name: "Jennine Cundict",
email: "[email protected]",
github: "jcundictl",
},
{
name: "Micaela Ebbotts",
email: "[email protected]",
github: "mebbottsm",
},
{
name: "Case Wilsher",
email: "[email protected]",
github: "cwilshern",
},
{
name: "Rubia Nielson",
email: "[email protected]",
github: "rnielsono",
},
{
name: "Darn Agget",
email: "[email protected]",
github: "daggetp",
},
{
name: "Lottie Gilligan",
email: "[email protected]",
github: "lgilliganq",
},
{
name: "Brucie Pollock",
email: "[email protected]",
github: "bpollockr",
},
{
name: "Laina Cripin",
email: "[email protected]",
github: "lcripins",
},
{
name: "Garth Padmore",
email: "[email protected]",
github: "gpadmoret",
},
{
name: "Madlin Boler",
email: "[email protected]",
github: "mboleru",
},
{
name: "Timmy Cobden",
email: "[email protected]",
github: "tcobdenv",
},
{
name: "Ingaborg Ashfold",
email: "[email protected]",
github: "iashfoldw",
},
{
name: "Mitzi Rollett",
email: "[email protected]",
github: "mrollettx",
},
{
name: "Keriann Spellworth",
email: "[email protected]",
github: "kspellworthy",
},
{
name: "Donovan Hucquart",
email: "[email protected]",
github: "dhucquartz",
},
{
name: "Saba Klaassens",
email: "[email protected]",
github: "sklaassens10",
},
{
name: "Niki Penella",
email: "[email protected]",
github: "npenella11",
},
{
name: "Gerda Moxsom",
email: "[email protected]",
github: "gmoxsom12",
},
{
name: "Dion Jakubiak",
email: "[email protected]",
github: "djakubiak13",
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ export async function POST(
) {
const { event } = params

const { name, email, github } = await request.json()
// TODO: validar com Zod
const {
name,
email,
github,
}: { name: string; email: string; github?: string } = await request.json()

const participant = await prisma.user.create({
data: {
name,
email,
github,
github: !!github ? github : undefined,
play: {
create: {
eventSlug: event,
Expand Down
24 changes: 12 additions & 12 deletions src/app/events/[event]/participants/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ export const upload = async (data: FormData) => {

const bytes = await file.arrayBuffer()
const csv = Buffer.from(bytes).toString()
try {
await saveInDb({ csv, event })
} catch (e) {
// TODO
console.log("deu ruim na hora de salvar no banco:", e)
}
await saveInDb({ csv, event })
}

type SaveInDbInput = {
Expand All @@ -46,14 +41,15 @@ type SaveInDbInput = {

async function saveInDb({ csv, event }: SaveInDbInput) {
const participants = csv.split("\n").slice(1)
const promiseParticipants = participants.map((line) => {
const [name, email, github] = line.split(";")
const participantsFiltered = participants.filter(Boolean)

const promiseParticipants = participantsFiltered.map((line) => {
const [, , , name, lastName, email] = line.split(",")

return prisma.user.create({
data: {
name,
email,
github,
name: name.trim() + " " + lastName.trim(),
email: email.trim(),
play: {
create: {
eventSlug: event,
Expand All @@ -62,5 +58,9 @@ async function saveInDb({ csv, event }: SaveInDbInput) {
},
})
})
return Promise.all(promiseParticipants)

const result = await Promise.allSettled(promiseParticipants)
result
.filter((r) => r.status === "rejected")
.forEach((r) => console.log("Deu ruim na hora de salvar no banco:", r))
}
2 changes: 1 addition & 1 deletion src/shared/types/participant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type Participant = z.infer<typeof participantOutsideGroupSchema>
export const registerParticipantSchema = z.object({
name: z.string().min(1, { message: "Name is required" }),
email: z.string().email().min(1, { message: "Email is required" }),
github: z.string().min(1, { message: "Github user is required" }),
github: z.string().optional(),
})

export type RegisterParticipant = z.infer<typeof registerParticipantSchema>
Expand Down
Loading