Skip to content

Commit

Permalink
feat: migrate to prisma
Browse files Browse the repository at this point in the history
- persist data in database
- add password authentication
- extract messages into own file
- refactor bot code

=> make app more robust
  • Loading branch information
ematala committed Apr 30, 2023
1 parent a1d2c22 commit e861ca2
Show file tree
Hide file tree
Showing 15 changed files with 454 additions and 136 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
TELEGRAM_BOT_TOKEN="YOUR_TELEGRAM_BOT_TOKEN"
TELEGRAM_BOT_TOKEN="YOUR_TELEGRAM_BOT_TOKEN"
DATABASE_URL="YOUR_DATABASE_URL"
PASSWORD="YOUR_PASSWORD"
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
out
.env
build
.env
*.db
TODO
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
"name": "putzbot",
"version": "1.0.0",
"description": "A telegram bot for cleaning duties",
"main": "out/index.js",
"main": "build/index.js",
"scripts": {
"dev": "ts-node src/index.ts",
"dev": "ts-node src --watch",
"build": "tsc",
"start": "tsc && node out/index.js"
"start": "tsc && node build"
},
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
"keywords": [
"telegram",
Expand All @@ -18,12 +21,15 @@
"author": "Eric Matala de Mazza",
"license": "MIT",
"devDependencies": {
"@types/lodash": "^4.14.194",
"@types/node": "^18.16.1",
"@types/node-cron": "^3.0.7",
"prisma": "^4.13.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
},
"dependencies": {
"@prisma/client": "4.13.0",
"date-fns": "^2.29.3",
"dotenv": "^16.0.3",
"node-cron": "^3.0.2",
Expand Down
43 changes: 43 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions prisma/migrations/20230430015513_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- CreateTable
CREATE TABLE "Roomie" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"done" BOOLEAN NOT NULL DEFAULT false,
"dutyId" INTEGER,
CONSTRAINT "Roomie_dutyId_fkey" FOREIGN KEY ("dutyId") REFERENCES "Duty" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Duty" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" TEXT NOT NULL,
"description" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "Trash" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "TrashCollection" (
"date" DATETIME NOT NULL PRIMARY KEY
);

-- CreateTable
CREATE TABLE "_TrashToTrashCollection" (
"A" INTEGER NOT NULL,
"B" DATETIME NOT NULL,
CONSTRAINT "_TrashToTrashCollection_A_fkey" FOREIGN KEY ("A") REFERENCES "Trash" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "_TrashToTrashCollection_B_fkey" FOREIGN KEY ("B") REFERENCES "TrashCollection" ("date") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "Roomie_dutyId_key" ON "Roomie"("dutyId");

-- CreateIndex
CREATE UNIQUE INDEX "_TrashToTrashCollection_AB_unique" ON "_TrashToTrashCollection"("A", "B");

-- CreateIndex
CREATE INDEX "_TrashToTrashCollection_B_index" ON "_TrashToTrashCollection"("B");
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"
38 changes: 38 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}

model Roomie {
id Int @id @default(autoincrement())
name String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
done Boolean @default(false)
dutyId Int? @unique
duty Duty? @relation(fields: [dutyId], references: [id])
}

model Duty {
id Int @id @default(autoincrement())
title String
description String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
roomie Roomie?
}

model Trash {
id Int @id @default(autoincrement())
title String
trashCollection TrashCollection[]
}

model TrashCollection {
date DateTime @id
trash Trash[]
}
71 changes: 71 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { PrismaClient } from "@prisma/client";
import { trash } from "../src/data";

const prisma = new PrismaClient();

const seed = async () => {
try {
await Promise.all(
[
{
title: "🛀🏽 Badezimmer",
description: "Badezimmer putzen",
},
{
title: "👨🏽‍🍳 Küche",
description: "Küche putzen",
},
{
title: "🧹 Wohnzimmer",
description: "Wohnzimmer aufräumen",
},
{
title: "🗑️ Müll",
description: "Müll rausbringen",
},
].map(async (data) => await prisma.duty.create({ data }))
);

await Promise.all(
[
"🟦 Papiertonne",
"⬛️ Restabfalltonne",
"🟫 Biotonne",
"🟨 Wertstofftonne",
"🟧 Sperrgut, Grünabfall",
].map(async (title) => await prisma.trash.create({ data: { title } }))
);

await Promise.all(
[...new Set(Object.keys(trash))].map(
async (key) =>
await prisma.trashCollection.create({
data: {
date: new Date(key),
},
})
)
);

// TODO @ematala implement/fix
// await Promise.all(
// Object.entries(trash).map(
// async ([key, values]) =>
// await prisma.trashCollection.update({
// where: {
// date: new Date(key),
// },
// data: {
// trash: {
// connect: values.map((id) => ({ id })),
// },
// },
// })
// )
// );
} catch (error) {
console.log(error);
}
};

seed();
1 change: 0 additions & 1 deletion src/constants.ts

This file was deleted.

42 changes: 6 additions & 36 deletions src/data.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
import type { Duty, Mapping, Roomie, Trash } from "./types";

export const roomies: Array<Roomie> = [];

export const duties: Array<Duty> = [
{
id: 0,
title: "🛀🏽 Badezimmer",
description: "Badezimmer putzen",
},
{
id: 1,
title: "👨🏽‍🍳 Küche",
description: "Küche putzen",
},
{
id: 2,
title: "🧹 Wohnzimmer",
description: "Wohnzimmer aufräumen",
},
{
id: 3,
title: "🗑️ Müll",
description: "Müll rausbringen",
},
];

export const mapping: Array<Mapping> = Array.from({
length: roomies.length,
}).map((_, i) => ({
roomie: roomies[i],
duty: duties[i],
done: false,
}));

export const trash: Trash = {
// 0: "🟦 Papiertonne",
// 1: "⬛️ Restabfalltonne",
// 2: "🟫 Biotonne",
// 3: "🟨 Wertstofftonne",
// 4: "🟧 Sperrgut, Grünabfall",
export const trash: Record<string, Array<number>> = {
"2023-05-04": [1, 2],
"2023-05-10": [0, 2, 3],
"2023-05-11": [4],
Expand Down
Loading

0 comments on commit e861ca2

Please sign in to comment.