Skip to content

Commit

Permalink
drop shareholder table, use address instead
Browse files Browse the repository at this point in the history
  • Loading branch information
labintsev committed Dec 15, 2024
1 parent 72c140e commit f9806a1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 51 deletions.
9 changes: 4 additions & 5 deletions packages/nextjs/app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ export default async function createAnswer(formData: FormData) {
question: formData.get("question"),
answer: formData.get("radio-0"),
};
// todo get id for current user address:
const sh_id = "d6e15727-9fe1-4961-8c5b-ea44a9bd8100";

const cookieStore = await cookies();
const connectedAddress = cookieStore.get(connectedAddressKey);
console.log(rawFormData, connectedAddress?.value);
const connectedAddress = cookieStore.get(connectedAddressKey)?.value;
console.log(rawFormData, connectedAddress);

await sql`
INSERT INTO answers (sh_id, question_id, choice_id, answer_time)
VALUES (${sh_id}, ${rawFormData.question?.toString()}, ${rawFormData.answer?.toString()}, now())
VALUES (${connectedAddress}, ${rawFormData.question?.toString()}, ${rawFormData.answer?.toString()}, now())
`;
redirect("/voting/results");
}
14 changes: 2 additions & 12 deletions packages/nextjs/app/seed/placeholder-data.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
const shareholders = [
{
id: "410544b2-4001-4271-9855-fec4b6a6442a",
name: "User",
email: "user@nextmail.com",
password: "123456",
},
];

const questions = [
{
id: "d6e15727-9fe1-4961-8c5b-ea44a9bd81a0",
Expand Down Expand Up @@ -65,12 +56,11 @@ const question_choices = [

const shareholder_question_answers = [
{
id: "d6e15727-9fe1-4961-8c5b-ea44a9bd8107",
sh_id: shareholders[0].id,
sh_id: "0x9eCf5353F6F09fac45F748E891EBC83095bbeAf8",
q_id: question_choices[0].id,
choice_id: question_choices[0].id,
answer_time: "2024-12-8 12-34",
},
];

export { shareholders, questions, question_choices, shareholder_question_answers };
export { questions, question_choices, shareholder_question_answers };
45 changes: 11 additions & 34 deletions packages/nextjs/app/seed/route.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,15 @@
import { question_choices, questions, shareholder_question_answers, shareholders } from "./placeholder-data";
import { sha256 } from "@noble/hashes/sha2";
import { bytesToHex } from "@noble/hashes/utils";
import { question_choices, questions, shareholder_question_answers } from "./placeholder-data";
import { db } from "@vercel/postgres";

const client = await db.connect();

async function seedShareholders() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
await client.sql`
CREATE TABLE IF NOT EXISTS shareholders (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
);
`;

const insertedShareholders = await Promise.all(
shareholders.map(async sholder => {
const hashedPassword = bytesToHex(sha256(sholder.password));
return client.sql`
INSERT INTO shareholders (id, name, email, password)
VALUES (${sholder.id}, ${sholder.name}, ${sholder.email}, ${hashedPassword})
ON CONFLICT (id) DO NOTHING;
`;
}),
);

return insertedShareholders;
}

async function seedQuestions() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;

await client.sql`
CREATE TABLE IF NOT EXISTS questions (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
question VARCHAR(255) NOT NULL,
question TEXT NOT NULL,
is_active INT NOT NULL
);
`;
Expand Down Expand Up @@ -84,18 +57,18 @@ async function seedShareholderAnswers() {
await client.sql`
CREATE TABLE IF NOT EXISTS answers (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
sh_id UUID NOT NULL,
sh_id VARCHAR(255) NOT NULL,
question_id UUID NOT NULL,
choice_id VARCHAR(255) NOT NULL,
choice_id UUID NOT NULL,
answer_time TIMESTAMP NOT NULL
);
`;

const insertedAnswers = await Promise.all(
shareholder_question_answers.map(
answer => client.sql`
INSERT INTO answers (id, sh_id, question_id, choice_id, answer_time)
VALUES (${answer.id}, ${answer.sh_id}, ${answer.q_id}, ${answer.choice_id}, now() )
INSERT INTO answers (sh_id, question_id, choice_id, answer_time)
VALUES (${answer.sh_id}, ${answer.q_id}, ${answer.choice_id}, now() )
ON CONFLICT (id) DO NOTHING;
`,
),
Expand All @@ -104,14 +77,18 @@ async function seedShareholderAnswers() {
return insertedAnswers;
}

async function clearTables() {
await client.sql`DROP TABLE IF EXISTS shareholders, questions, choices, answers;`;
}

export async function GET() {
// return Response.json({
// message:
// 'Uncomment this file and remove this line. You can delete this file when you are finished.',
// });
try {
await client.sql`BEGIN`;
await seedShareholders();
await clearTables();
await seedQuestions();
await seedChoices();
await seedShareholderAnswers();
Expand Down

0 comments on commit f9806a1

Please sign in to comment.