-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
347 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
ALTER TABLE `chains` RENAME COLUMN "createdAt" TO "created_at";--> statement-breakpoint | ||
ALTER TABLE `chains` RENAME COLUMN "updatedAt" TO "updated_at";--> statement-breakpoint | ||
CREATE TABLE `users` ( | ||
`id` text PRIMARY KEY NOT NULL, | ||
`resource_type` text DEFAULT 'user' NOT NULL, | ||
`created_at` text DEFAULT (CURRENT_TIMESTAMP) NOT NULL, | ||
`created_by` text, | ||
`updated_at` text DEFAULT (CURRENT_TIMESTAMP) NOT NULL, | ||
`telegram_chat_id` integer NOT NULL, | ||
FOREIGN KEY (`created_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action | ||
); | ||
--> statement-breakpoint | ||
CREATE UNIQUE INDEX `users_telegramChatId_unique` ON `users` (`telegram_chat_id`);--> statement-breakpoint | ||
ALTER TABLE `chains` ADD `resource_type` text DEFAULT 'chain' NOT NULL;--> statement-breakpoint | ||
ALTER TABLE `chains` ADD `created_by` text NOT NULL REFERENCES users(id);--> statement-breakpoint | ||
ALTER TABLE `chains` ADD `name` text NOT NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
{ | ||
"version": "6", | ||
"dialect": "sqlite", | ||
"id": "2b4f22d3-5a7a-44b0-85f8-0c48a86ba4e3", | ||
"prevId": "73990216-a750-4ce5-b29d-8a573b6ceae1", | ||
"tables": { | ||
"chains": { | ||
"name": "chains", | ||
"columns": { | ||
"id": { | ||
"name": "id", | ||
"type": "text", | ||
"primaryKey": true, | ||
"notNull": true, | ||
"autoincrement": false | ||
}, | ||
"resource_type": { | ||
"name": "resource_type", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false, | ||
"default": "'chain'" | ||
}, | ||
"created_at": { | ||
"name": "created_at", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false, | ||
"default": "(CURRENT_TIMESTAMP)" | ||
}, | ||
"created_by": { | ||
"name": "created_by", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false | ||
}, | ||
"updated_at": { | ||
"name": "updated_at", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false, | ||
"default": "(CURRENT_TIMESTAMP)" | ||
}, | ||
"name": { | ||
"name": "name", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false | ||
} | ||
}, | ||
"indexes": {}, | ||
"foreignKeys": { | ||
"chains_created_by_users_id_fk": { | ||
"name": "chains_created_by_users_id_fk", | ||
"tableFrom": "chains", | ||
"tableTo": "users", | ||
"columnsFrom": ["created_by"], | ||
"columnsTo": ["id"], | ||
"onDelete": "no action", | ||
"onUpdate": "no action" | ||
} | ||
}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {}, | ||
"checkConstraints": {} | ||
}, | ||
"users": { | ||
"name": "users", | ||
"columns": { | ||
"id": { | ||
"name": "id", | ||
"type": "text", | ||
"primaryKey": true, | ||
"notNull": true, | ||
"autoincrement": false | ||
}, | ||
"resource_type": { | ||
"name": "resource_type", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false, | ||
"default": "'user'" | ||
}, | ||
"created_at": { | ||
"name": "created_at", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false, | ||
"default": "(CURRENT_TIMESTAMP)" | ||
}, | ||
"created_by": { | ||
"name": "created_by", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": false, | ||
"autoincrement": false | ||
}, | ||
"updated_at": { | ||
"name": "updated_at", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false, | ||
"default": "(CURRENT_TIMESTAMP)" | ||
}, | ||
"telegram_chat_id": { | ||
"name": "telegram_chat_id", | ||
"type": "integer", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false | ||
} | ||
}, | ||
"indexes": { | ||
"users_telegramChatId_unique": { | ||
"name": "users_telegramChatId_unique", | ||
"columns": ["telegram_chat_id"], | ||
"isUnique": true | ||
} | ||
}, | ||
"foreignKeys": { | ||
"users_created_by_users_id_fk": { | ||
"name": "users_created_by_users_id_fk", | ||
"tableFrom": "users", | ||
"tableTo": "users", | ||
"columnsFrom": ["created_by"], | ||
"columnsTo": ["id"], | ||
"onDelete": "no action", | ||
"onUpdate": "no action" | ||
} | ||
}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {}, | ||
"checkConstraints": {} | ||
} | ||
}, | ||
"views": {}, | ||
"enums": {}, | ||
"_meta": { | ||
"schemas": {}, | ||
"tables": {}, | ||
"columns": { | ||
"\"chains\".\"createdAt\"": "\"chains\".\"created_at\"", | ||
"\"chains\".\"updatedAt\"": "\"chains\".\"updated_at\"" | ||
} | ||
}, | ||
"internal": { | ||
"indexes": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { Hono } from "hono"; | ||
import { listChains } from "../../bl/chains/chains.bl.ts"; | ||
import { findChains } from "../../bl/chains/chains.bl.ts"; | ||
|
||
export const chainsRoute = new Hono(); | ||
|
||
chainsRoute.get("/", async (hc) => hc.json(await listChains())); | ||
chainsRoute.get("/", async (hc) => hc.json(await findChains())); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Composer } from "grammy"; | ||
import { getContext } from "hono/context-storage"; | ||
import { findOrCreateUserByTelegramChatId } from "../../bl/users/users.bl.ts"; | ||
import type { Context } from "../../shared/context/context.ts"; | ||
|
||
export const authComposer = new Composer(); | ||
|
||
authComposer.use(async (tc, next) => { | ||
const hc = getContext<Context>(); | ||
|
||
if (!tc.chat) { | ||
throw new Error("Expect chat to be defined!"); | ||
} | ||
|
||
const user = await findOrCreateUserByTelegramChatId(tc.chat.id); | ||
hc.set("user", user); | ||
|
||
await next(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import { Composer } from "grammy"; | ||
import { getContext } from "hono/context-storage"; | ||
import type { Context } from "../../shared/context/context.ts"; | ||
|
||
export const echoComposer = new Composer(); | ||
|
||
echoComposer.on("message", (tc) => { | ||
return tc.reply(tc.message.text ?? "no text in your message"); | ||
const { user } = getContext<Context>().var; | ||
|
||
return tc.reply( | ||
`you are ${user.id} and you wrote ${tc.message.text ? `"${tc.message.text}"` : "nothing"}`, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { selectChains } from "../../dal/chains/chains.table.ts"; | ||
import { findAllChains } from "../../dal/chains/chains.table.ts"; | ||
import type { Chain } from "../../shared/schema/chains.ts"; | ||
|
||
export async function listChains(): Promise<Array<Chain>> { | ||
return await selectChains(); | ||
export async function findChains(): Promise<Array<Chain>> { | ||
return await findAllChains(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
createUser, | ||
findUserByTelegramChatId, | ||
} from "../../dal/users/users.table.ts"; | ||
import type { User } from "../../shared/schema/users.ts"; | ||
|
||
export async function findOrCreateUserByTelegramChatId( | ||
telegramChatId: User["telegramChatId"], | ||
): Promise<User> { | ||
const existing = await findUserByTelegramChatId(telegramChatId); | ||
if (existing) { | ||
return existing; | ||
} | ||
|
||
return await createUser({ telegramChatId }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
import { getContext } from "hono/context-storage"; | ||
import type { Context } from "../../shared/context/context.ts"; | ||
import { | ||
type Chain, | ||
chains, | ||
selectChainSchema, | ||
} from "../../shared/schema/chains.ts"; | ||
import { type Chain, chainSchema, chains } from "../../shared/schema/chains.ts"; | ||
|
||
export async function selectChains(): Promise<Array<Chain>> { | ||
export async function findAllChains(): Promise<Array<Chain>> { | ||
const { db } = getContext<Context>().var; | ||
|
||
return (await db.select().from(chains)).map((chain) => | ||
selectChainSchema.parse(chain), | ||
chainSchema.parse(chain), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { sql } from "drizzle-orm"; | ||
import { getContext } from "hono/context-storage"; | ||
import type { Context } from "../../shared/context/context.ts"; | ||
import { | ||
type InsertUser, | ||
type User, | ||
createUserSchema, | ||
userSchema, | ||
users, | ||
} from "../../shared/schema/users.ts"; | ||
|
||
export async function createUser(raw: InsertUser): Promise<User> { | ||
const { db, user } = getContext<Context>().var; | ||
|
||
const toCreate = createUserSchema.parse(raw); | ||
|
||
return userSchema.parse( | ||
await db | ||
.insert(users) | ||
.values({ ...toCreate, createdBy: user?.id }) | ||
.returning(), | ||
); | ||
} | ||
|
||
export async function findUserByTelegramChatId( | ||
telegramChatId: User["telegramChatId"], | ||
): Promise<User | null> { | ||
const { db } = getContext<Context>().var; | ||
|
||
const [raw] = await db | ||
.select() | ||
.from(users) | ||
.where(sql`${users.telegramChatId} = ${telegramChatId}`); | ||
|
||
return raw ? userSchema.parse(raw) : null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.