Skip to content

Commit

Permalink
add: slack notify
Browse files Browse the repository at this point in the history
  • Loading branch information
toririm committed Sep 20, 2024
1 parent cbff0b7 commit 5091586
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ node_modules
/.cache
/build
/public/build
.env
.env*
!.env.example

.firebase
.firebaserc
Expand Down
19 changes: 19 additions & 0 deletions app/lib/webhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const sendSlackMessage = async (message: string) => {
const webhookUrl = import.meta.env.WEBHOOK_URL;

if (!webhookUrl) {
throw new Error("WEBHOOK_URL is not defined");
}

const response = await fetch(webhookUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ text: message }),
});

if (!response.ok) {
throw new Error("Failed to send message");
}
};
7 changes: 6 additions & 1 deletion app/routes/items/actions/addItem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parseWithZod } from "@conform-to/zod";
import { type ClientActionFunction, json } from "@remix-run/react";
import { sendSlackMessage } from "~/lib/webhook";
import { ItemEntity, itemSchema } from "~/models/item";
import { itemRepository } from "~/repositories/item";

Expand All @@ -14,7 +15,11 @@ export const addItem: ClientActionFunction = async ({ request }) => {
}

const newItem = ItemEntity.createNew(submission.value);
const savedItem = await itemRepository.save(newItem);
const itemSavePromise = itemRepository.save(newItem);
const webhookSendPromise = sendSlackMessage(
`新しいアイテムが追加されました!\n${newItem.name}`,
);
const [savedItem] = await Promise.all([itemSavePromise, webhookSendPromise]);

console.log("Document written with ID: ", savedItem.id);
return json(submission.reply({ resetForm: true }), { status: 200 });
Expand Down
9 changes: 9 additions & 0 deletions app/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
readonly WEBHOOK_URL: string;
}

interface ImportMeta {
readonly env: ImportMetaEnv;
}

0 comments on commit 5091586

Please sign in to comment.