Skip to content

Commit e01adf2

Browse files
committed
Fix double insertion bug
1 parent fea3fcb commit e01adf2

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

src/commands/boobs.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { time, TimestampStyles, type User } from "discord.js";
22

33
import type { ProcessableMessage } from "@/service/command.js";
44
import type { MessageCommand } from "@/commands/command.js";
5+
import type { Boob } from "@/storage/db/model.js";
56
import * as boob from "@/storage/boob.js";
67
import log from "@log";
78
import { randomEntry } from "@/service/random.js";
@@ -141,29 +142,33 @@ export default class BoobCommand implements MessageCommand {
141142
const mention = message.mentions.users.first();
142143
const userToMeasure = mention !== undefined ? mention : author;
143144

144-
log.debug(`${author.id} wants to measure boob of user ${userToMeasure.id}`);
145-
146-
const recentMeasurement = await boob.fetchRecentMeasurement(userToMeasure);
147-
148-
if (recentMeasurement === undefined) {
149-
log.debug(
150-
`No recent boob measuring of ${userToMeasure.id} found. Creating Measurement`,
151-
);
152-
153-
const size = Number(randomEntry(Object.keys(boobas)));
154-
155-
await Promise.all([
156-
boob.insertMeasurement(userToMeasure, size),
157-
sendBoob(userToMeasure, message, size),
158-
]);
159-
return;
160-
}
145+
log.debug(`${author.id} wants to measure boobs of user ${userToMeasure.id}`);
146+
const measurement = await this.#getOrCreateMeasurement(userToMeasure);
161147

162148
await sendBoob(
163149
userToMeasure,
164150
message,
165-
recentMeasurement.size,
166-
new Date(`${recentMeasurement.measuredAt}Z`),
151+
measurement.size,
152+
new Date(`${measurement.measuredAt}Z`),
167153
);
168154
}
155+
156+
async #getOrCreateMeasurement(userToMeasure: User): Promise<Boob> {
157+
const lastMeasurement = await boob.fetchLastMeasurement(userToMeasure);
158+
159+
if (lastMeasurement !== undefined) {
160+
const now = new Date();
161+
const measurement = new Date(`${lastMeasurement.measuredAt}Z`);
162+
// TODO: Make use of temporal lol
163+
if (measurement.toISOString().split("T")[0] === now.toISOString().split("T")[0]) {
164+
return lastMeasurement;
165+
}
166+
}
167+
168+
log.debug(`No recent boob measuring of ${userToMeasure.id} found. Creating Measurement`);
169+
170+
const size = Number(randomEntry(Object.keys(boobas)));
171+
172+
return await boob.insertMeasurement(userToMeasure, size);
173+
}
169174
}

src/storage/boob.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function insertMeasurement(
2525
.executeTakeFirstOrThrow();
2626
}
2727

28-
export function fetchRecentMeasurement(user: User, ctx = db()): Promise<Boob | undefined> {
28+
export function fetchLastMeasurement(user: User, ctx = db()): Promise<Boob | undefined> {
2929
const now = Temporal.Now.instant();
3030
const { startOfToday, startOfTomorrow } = getStartAndEndDay(now);
3131

0 commit comments

Comments
 (0)