diff --git a/.gitignore b/.gitignore index cb847c5..a3fb00c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ dist-app dist-auth dist-worker .dev.vars +test/support/test.sqlite diff --git a/migrations/0000_create_user_tables.sql b/migrations/0000_create_user_tables.sql index 09d9f7c..9efda0c 100644 --- a/migrations/0000_create_user_tables.sql +++ b/migrations/0000_create_user_tables.sql @@ -1,5 +1,3 @@ --- Migration number: 0000 2024-02-13T03:45:01.307Z - create table users ( id integer primary key, diff --git a/src/accounts/accountsGateway.ts b/src/accounts/accountsGateway.ts index f540b3c..1ee4225 100644 --- a/src/accounts/accountsGateway.ts +++ b/src/accounts/accountsGateway.ts @@ -1,15 +1,15 @@ export type AccountRecord = { - id: string + id: number name: string } export type AccountsGateway = { - findForUser: (userId: string) => Promise + findForUser: (userId: number) => Promise create: (name: string) => Promise } export const accountsGateway = (db: D1Database): AccountsGateway => ({ - findForUser: async (userId: string): Promise => { + findForUser: async (userId: number): Promise => { const record = await db.prepare(` select a.id, a.name from accounts a join main.memberships m on a.id = m.account_id @@ -18,7 +18,7 @@ export const accountsGateway = (db: D1Database): AccountsGateway => ({ if (record === null) return record return { - id: record["id"] as string, + id: record["id"] as number, name: record["name"] as string, } }, @@ -29,7 +29,7 @@ export const accountsGateway = (db: D1Database): AccountsGateway => ({ } return { - id: record["id"] as string, + id: record["id"] as number, name: record["name"] as string, } } diff --git a/src/accounts/accountsService.ts b/src/accounts/accountsService.ts index 1f14485..14ce508 100644 --- a/src/accounts/accountsService.ts +++ b/src/accounts/accountsService.ts @@ -3,9 +3,9 @@ import {UsersGateway} from "./usersGateway"; import {AccountRecord, AccountsGateway} from "./accountsGateway"; export type UserAccount = { - id: string + id: number email: string - accountId: string + accountId: number accountName: string } @@ -18,7 +18,7 @@ export const accountsService = ( usersGateway: UsersGateway, membershipsGateway: MembershipsGateway, ): AccountsService => { - const createAccountFor = async (userId: string, email: string): Promise => { + const createAccountFor = async (userId: number, email: string): Promise => { const account = await accountsGateway.create(`${email} account`) await membershipsGateway.create(userId, account.id) return account diff --git a/src/accounts/membershipsGateway.ts b/src/accounts/membershipsGateway.ts index ec6903a..64439a3 100644 --- a/src/accounts/membershipsGateway.ts +++ b/src/accounts/membershipsGateway.ts @@ -1,15 +1,15 @@ type MembershipRecord = { - id: string - userId: string - accountId: string + id: number + userId: number + accountId: number } export type MembershipsGateway = { - create: (userId: string, accountId: string) => Promise + create: (userId: number, accountId: number) => Promise } export const membershipsGateway = (db: D1Database): MembershipsGateway => ({ - create: async (userId: string, accountId: string): Promise => { + create: async (userId: number, accountId: number): Promise => { const record = await db.prepare(` insert into memberships (user_id, account_id, owner) values (?, ?, true) @@ -21,9 +21,9 @@ export const membershipsGateway = (db: D1Database): MembershipsGateway => ({ } return { - id: record["id"] as string, - userId: record["user_id"] as string, - accountId: record["account_id"] as string, + id: record["id"] as number, + userId: record["user_id"] as number, + accountId: record["account_id"] as number, } } }) diff --git a/src/accounts/usersGateway.ts b/src/accounts/usersGateway.ts index f8a78e1..b8a41f1 100644 --- a/src/accounts/usersGateway.ts +++ b/src/accounts/usersGateway.ts @@ -1,5 +1,5 @@ type UserRecord = { - id: string + id: number email: string } @@ -14,7 +14,7 @@ export const usersGateway = (db: D1Database): UsersGateway => ({ if (record === null) return record return { - id: record["id"] as string, + id: record["id"] as number, email: record["email"] as string, } }, @@ -25,7 +25,7 @@ export const usersGateway = (db: D1Database): UsersGateway => ({ } return { - id: record["id"] as string, + id: record["id"] as number, email: record["email"] as string, } } diff --git a/src/auth/session.ts b/src/auth/session.ts index 46d4701..28a8334 100644 --- a/src/auth/session.ts +++ b/src/auth/session.ts @@ -1,6 +1,6 @@ import {UserAccount} from "../accounts/accountsService"; -type UserSession = { userId: string, email: string, accountId: string, accountName: string }; +type UserSession = { userId: number, email: string, accountId: number, accountName: string }; type EmptySession = { userId: null, email: null, accountId: null, accountName: null }; export type Session = UserSession | EmptySession diff --git a/test/accounts/membershipsGateway.test.ts b/test/accounts/membershipsGateway.test.ts new file mode 100644 index 0000000..8011528 --- /dev/null +++ b/test/accounts/membershipsGateway.test.ts @@ -0,0 +1,24 @@ +import {membershipsGateway} from "../../src/accounts/membershipsGateway"; +import {clear, createDb, migrate} from "../support/databaseSupport"; +import {beforeEach} from "vitest"; + +describe("membershipsGateway", async () => { + const db = createDb() + await migrate(db) + + beforeEach(async () => { + await clear(db) + }); + + test("create", async () => { + const gateway = membershipsGateway(db) + + await db.exec("insert into users (id, email) values (11, 'test@example.com');") + await db.exec("insert into accounts (id, name) values (22, 'Some account');") + + const result = await gateway.create(11, 22) + + expect(result["userId"]).toEqual(11) + expect(result["accountId"]).toEqual(22) + }); +}); diff --git a/test/support/databaseSupport.ts b/test/support/databaseSupport.ts new file mode 100644 index 0000000..550e328 --- /dev/null +++ b/test/support/databaseSupport.ts @@ -0,0 +1,25 @@ +import fs from "fs"; +import Database from "better-sqlite3"; +import {D1Database, D1DatabaseAPI} from "@miniflare/d1"; +import {D1Database as D1DatabaseType} from "@cloudflare/workers-types" + +export const createDb = (): D1DatabaseType => { + fs.rmSync('test/support/test.sqlite', {force: true}) + const sqlite = new Database('test/support/test.sqlite'); + const api = new D1DatabaseAPI(sqlite) + return new D1Database(api) as D1DatabaseType +} + +export const migrate = async (db: D1DatabaseType): Promise => { + const sql = fs.readFileSync('migrations/0000_create_user_tables.sql', {encoding: "utf8"}); + const statements = sql.replaceAll('\n', '').split(';').filter(statement => statement !== ''); + for (const statement of statements) { + await db.exec(`${statement};`) + } +} + +export const clear = async (db: D1DatabaseType): Promise => { + await db.exec('delete from memberships') + await db.exec('delete from accounts') + await db.exec('delete from users') +}