Skip to content

Commit

Permalink
🧹 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kawamataryo committed Mar 3, 2024
1 parent d158268 commit 64bab05
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
15 changes: 5 additions & 10 deletions src/interactions/modalSubmits/checkinModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,12 @@ const handler = async ({
const profile = intentObj.data.components[0].components[0].value;
const todo = intentObj.data.components[1].components[0].value;

const targetUsers = await usersRepository.findByDiscordUserId(
intentObj.member.user.id,
);

const user =
targetUsers.length > 0
? targetUsers[0]
: await usersRepository.create({
name: intentObj.member.user.username,
discordUserId: intentObj.member.user.id,
});
(await usersRepository.findUserByDiscordUserId(intentObj.member.user.id)) ??
(await usersRepository.create({
name: intentObj.member.user.username,
discordUserId: intentObj.member.user.id,
}));
const checkin = await checkinsRepository.create({
userId: user.id,
profile,
Expand Down
12 changes: 5 additions & 7 deletions src/repositories/usersRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import { checkins, users } from "../schema";
import { BaseRepository } from "./baseRepository";

export class UsersRepository extends BaseRepository {
async findByDiscordUserId(discordUserId: string) {
const targetUsers = await this.db
.select()
.from(users)
.where(eq(users.discordUserId, discordUserId));
return targetUsers;
async findUserByDiscordUserId(discordUserId: string) {
return await this.db.query.users.findFirst({
where: eq(users.discordUserId, discordUserId),
});
}

async create({
Expand Down Expand Up @@ -45,6 +43,6 @@ export class UsersRepository extends BaseRepository {
.where(eq(users.discordUserId, discordUserId))
.orderBy(desc(checkins.createdAt))
.limit(1);
return result.length > 0 ? result[0] : null;
return result.length > 0 ? result[0] : undefined;
}
}
4 changes: 4 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export const events = sqliteTable("events", {
createdAt: text("created_at").default(sql`CURRENT_TIMESTAMP`),
});

export const usersRelations = relations(users, ({ many }) => ({
checkins: many(checkins),
}));

export const eventsRelations = relations(events, ({ many }) => ({
eventsToCheckins: many(eventsToCheckins),
}));
Expand Down

0 comments on commit 64bab05

Please sign in to comment.