Skip to content

Commit

Permalink
Merge pull request #77 from dotslashf/development
Browse files Browse the repository at this point in the history
Reset Streak
  • Loading branch information
dotslashf authored Sep 25, 2024
2 parents d10a55c + 4d62e49 commit a2a91d9
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 13 deletions.
37 changes: 37 additions & 0 deletions cron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import cron from "node-cron";
import fetch from "node-fetch";

const CRON_SECRET = process.env.CRON_SECRET;
const API_URL = process.env.NEXT_PUBLIC_API_URL;

// Run every day at midnight Jakarta time (5pm UTC)
cron.schedule(
"0 17 * * *",
async () => {
try {
const response = await fetch(
`${API_URL}/api/trpc/cron.dailyStreakReset`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
json: {
secret: CRON_SECRET,
},
}),
},
);

const result = await response.json();
console.log("Cron job result:", result);
} catch (error) {
console.error("Error running cron job:", error);
}
},
{
scheduled: true,
timezone: "UTC",
},
);
183 changes: 171 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"clsx": "^2.1.1",
"cmdk": "^1.0.0",
"date-fns": "^3.6.0",
"date-fns-tz": "^3.1.3",
"framer-motion": "^11.3.24",
"geist": "^1.3.0",
"googleapis": "^143.0.0",
Expand All @@ -62,6 +63,8 @@
"next-auth": "^4.24.7",
"next-themes": "^0.3.0",
"nextjs-toploader": "^3.6.15",
"node-cron": "^3.0.3",
"node-fetch": "^3.3.2",
"react": "^18.3.1",
"react-day-picker": "8.10.1",
"react-dom": "^18.3.1",
Expand All @@ -81,6 +84,7 @@
"devDependencies": {
"@types/eslint": "^8.56.10",
"@types/node": "^20.14.10",
"@types/node-cron": "^3.0.11",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/uuid": "^10.0.0",
Expand Down
2 changes: 2 additions & 0 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const env = createEnv({
GCP_PROJECT_ID: z.string(),
GOOGLE_APPLICATION_CREDENTIALS: z.string(),
GCS_BUCKET_NAME: z.string(),
CRON_SECRET: z.string(),
},

/**
Expand Down Expand Up @@ -64,6 +65,7 @@ export const env = createEnv({
GCP_PROJECT_ID: process.env.GCP_PROJECT_ID,
GCS_BUCKET_NAME: process.env.GCS_BUCKET_NAME,
GOOGLE_APPLICATION_CREDENTIALS: process.env.GOOGLE_APPLICATION_CREDENTIALS,
CRON_SECRET: process.env.CRON_SECRET,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
Expand Down
2 changes: 2 additions & 0 deletions src/lib/constant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,5 @@ export const ENGAGEMENT_SCORE = {
DeleteReaction: -3,
DeleteCollection: -3,
};

export const TIMEZONE = "Asia/Jakarta";
11 changes: 10 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { twMerge } from "tailwind-merge";
import { type $Enums, EngagementAction, OriginSource } from "@prisma/client";
import { type ClassValue, clsx } from "clsx";
import { format, formatDistance, parse } from "date-fns";
import { format as formatTz, toZonedTime } from "date-fns-tz";
import { id } from "date-fns/locale";
import {
type Breadcrumb,
Expand All @@ -14,7 +15,7 @@ import {
type WebSite,
type SearchAction,
} from "schema-dts";
import { baseUrl } from "./constant";
import { baseUrl, TIMEZONE } from "./constant";

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
Expand All @@ -32,6 +33,14 @@ export function parseDate(date: string) {
return parse(date, "d MMMM yyyy", new Date(), { locale: id });
}

export function getJakartaDate(date: Date = new Date()): Date {
return toZonedTime(date, TIMEZONE);
}

export function getJakartaDateString(date: Date = new Date()): string {
return formatTz(getJakartaDate(date), "yyyy-MM-dd", { timeZone: TIMEZONE });
}

export function trimContent(content: string, length = 255) {
if (!content) {
return "😱😱😱";
Expand Down
Loading

0 comments on commit a2a91d9

Please sign in to comment.