Skip to content

Commit

Permalink
fix sending message error
Browse files Browse the repository at this point in the history
  • Loading branch information
Nusab19 committed Aug 22, 2024
1 parent 7e1d3dd commit f033729
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 62 deletions.
47 changes: 27 additions & 20 deletions src/app/api/others/send/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { NextRequest } from "next/server";
// import { escapeHTML } from "@/lib/MdtoHtml";
import { JsonResponse } from "../../default";"../../default";"../../default";"../../default";
import { JsonResponse } from "../../default";
("../../default");
("../../default");
("../../default");

const URL = process.env.URL;

export async function POST(req: NextRequest) {
console.log("Hello?")
const ip = req.headers.get("x-real-ip") ?? "127.0.0.1";
const jsonData = await req.json();
const message = String(jsonData.message).trim();
Expand All @@ -16,6 +20,12 @@ export async function POST(req: NextRequest) {
};
return await JsonResponse(data, 400);
}

const result = await sendToAuthor(message, ip);
return await JsonResponse(result, result.status_code);
}

export async function sendToAuthor(message: string, ip: string) {
const content = `
📧 New message from ${ip}
Message:
Expand All @@ -30,29 +40,26 @@ ${message}
const data = await response.json();

if (data.ok) {
return await JsonResponse({
return {
ok: true,
status_code: 200,
message: "Message sent successfully!",
description: "Thanks for your feedback."
});
description: "Thanks for your feedback.",
};
}

return await JsonResponse(
{
ok: false,
message: "Message wasn't sent!",
description: "Maybe poor connection? Please try again!"
},
400,
);
return {
ok: false,
status_code: 400,
message: "Message wasn't sent!",
description: "Maybe poor connection? Please try again!",
};
} catch (error) {
return await JsonResponse(
{
ok: false,
message: "Something went wrong.",
description: "Server failed to respond. Please try again!"
},
500,
);
return {
ok: false,
status_code: 500,
message: "Something went wrong.",
description: "Server failed to respond. Please try again!",
};
}
}
53 changes: 11 additions & 42 deletions src/lib/helpers/server.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
"use server";
import { randomInt } from "@/lib/utils";
import getFakeContestData from "@/lib/helpers/fakeContestData";
import { sendToAuthor } from "@/app/api/others/send/route";
("use server");
import { updateData as updateStatsData } from "@/lib/dbConnect";
import { getResponse as getContestResponse } from "@/app/api/default";
import { getSecondsDifferencesFromCurrentTime } from "@/lib/helpers/datetime";

import { ContestDataType } from "@/lib/types";

const OFFLINE = process.env.OFFLINE;

function _getRandomStats() {
return [
{
title: "Today",
value: randomInt(100),
description: "visited",
},
{
title: "Total served",
value: randomInt(1000000),
description: "API requests",
},
{
title: "Total",
value: randomInt(5000000),
description: "page visits",
},
];
}

export async function getStatsData(update: "api" | "page") {
if (OFFLINE) {
return _getRandomStats();
}

// also increments whenever called
const data = await updateStatsData(update);
return [
Expand All @@ -56,20 +29,16 @@ export async function getStatsData(update: "api" | "page") {
}

export async function sendMessage(message: string) {
if (OFFLINE)
return {
ok: true,
message: "Sent Message to Nowhere!",
description: "You are offline right now. You know that?",
};

const response = await fetch("/api/others/send", {
method: "POST",
headers: {
"Content-Type": "application/json",
const response = await fetch(
"https://contest-hive.vercel.app/api/others/send",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message }),
},
body: JSON.stringify({ message }),
});
);

return response.json();
}
Expand Down

0 comments on commit f033729

Please sign in to comment.