Skip to content

Commit

Permalink
fix: proper transaction handling
Browse files Browse the repository at this point in the history
  • Loading branch information
didinele committed Oct 10, 2023
1 parent 740e0dc commit df9e192
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
17 changes: 15 additions & 2 deletions packages/bot/src/commands/ask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ export default class implements Command<ApplicationCommandType.ChatInput> {
};

// eslint-disable-next-line unicorn/consistent-function-scoping
const unwrapErr = (result: Result<unknown, Error>) => {
const unwrapErr = <T>(result: Result<T, Error>): T => {
if (result.isErr()) {
const err = result.unwrapErr();
throw new GracefulTransactionFailure(err.message, { cause: err });
}

return result.unwrap();
};

if (ama.modQueue) {
Expand All @@ -112,13 +114,24 @@ export default class implements Command<ApplicationCommandType.ChatInput> {
}),
);
} else {
unwrapErr(
const message = unwrapErr(
await this.amaManager.postToAnswersChannel({
...basePostData,
answersChannel: ama.answersChannel,
stage: ama.stageOnly,
}),
);

if (message) {
await this.prisma.amaQuestion.update({
where: {
id: amaQuestion.id,
},
data: {
answerMessageId: message.id,
},
});
}
}

return amaQuestion;
Expand Down
9 changes: 9 additions & 0 deletions packages/bot/src/components/guest-approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ export default class implements Component<ButtonInteraction<'cached'>> {
content: result.unwrapErr().message,
ephemeral: true,
});
} else {
await this.prisma.amaQuestion.update({
where: {
id: question.id,
},
data: {
answerMessageId: result.unwrap().id,
},
});
}

return interaction.update({
Expand Down
9 changes: 9 additions & 0 deletions packages/bot/src/components/mod-approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ export default class implements Component<ButtonInteraction<'cached'>> {
content: result.unwrapErr().message,
ephemeral: true,
});
} else {
await this.prisma.amaQuestion.update({
where: {
id: question.id,
},
data: {
answerMessageId: result.unwrap().id,
},
});
}
}

Expand Down
17 changes: 15 additions & 2 deletions packages/bot/src/components/submit-question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ export default class implements Component<ButtonInteraction<'cached'>> {
};

// eslint-disable-next-line unicorn/consistent-function-scoping
const unwrapErr = (result: Result<unknown, Error>) => {
const unwrapErr = <T>(result: Result<T, Error>): T => {
if (result.isErr()) {
const err = result.unwrapErr();
throw new GracefulTransactionFailure(err.message, { cause: err });
}

return result.unwrap();
};

if (ama.modQueue) {
Expand All @@ -118,13 +120,24 @@ export default class implements Component<ButtonInteraction<'cached'>> {
}),
);
} else {
unwrapErr(
const message = unwrapErr(
await this.amaManager.postToAnswersChannel({
...basePostData,
answersChannel: ama.answersChannel,
stage: ama.stageOnly,
}),
);

if (message) {
await this.prisma.amaQuestion.update({
where: {
id: amaQuestion.id,
},
data: {
answerMessageId: message.id,
},
});
}
}

return amaQuestion;
Expand Down
15 changes: 3 additions & 12 deletions packages/bot/src/struct/AmaManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrismaClient } from '@prisma/client';
import type { AmaQuestion } from '@prisma/client';
import { Result } from '@sapphire/result';
import type { MessageActionRowComponentBuilder, TextChannel, User } from 'discord.js';
import type { Message, MessageActionRowComponentBuilder, TextChannel, User } from 'discord.js';
import { ActionRowBuilder, ButtonBuilder, EmbedBuilder, ButtonStyle, Client } from 'discord.js';
import { singleton } from 'tsyringe';
import { Colors } from '../util/colors.js';
Expand Down Expand Up @@ -136,7 +136,7 @@ export class AmaManager {
stage,
answersChannel,
...embedData
}: PostToAnswerChannelData): Promise<Result<unknown, Error>> {
}: PostToAnswerChannelData): Promise<Result<Message<true>, Error>> {
const embed = this.getBaseEmbed(embedData);
embed.setColor(Colors.Blurple);

Expand All @@ -155,15 +155,6 @@ export class AmaManager {
embeds: [embed],
});

await this.prisma.amaQuestion.update({
where: {
id: question.id,
},
data: {
answerMessageId: message.id,
},
});

return Result.ok();
return Result.ok(message);
}
}

0 comments on commit df9e192

Please sign in to comment.