aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2021-04-19 15:29:33 +0300
committerJan Tuomi <jan.tuomi@valuemotive.com>2021-04-19 15:29:33 +0300
commit2c5c47e3c5343419a6efa618b9d6549394da2e9f (patch)
tree103498479f2fa5d7995b7bf529124c733343439c /src
parent7f3a70c396b498a8b10553d11cf78c3ea9a872b1 (diff)
Fix date logic
Diffstat (limited to 'src')
-rw-r--r--src/config.ts6
-rw-r--r--src/index.ts1
-rw-r--r--src/middleware.ts6
-rw-r--r--src/sheets.ts6
4 files changed, 16 insertions, 3 deletions
diff --git a/src/config.ts b/src/config.ts
index 8766db9..ca60b97 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -13,6 +13,9 @@ class ConfigError extends Error {
}
}
+const nodeEnv = process.env.NODE_ENV || "development";
+const port = process.env.PORT ? Number(process.env.PORT) : 3000;
+
const tgWebhookUrl = process.env.TELEGRAM_WEBHOOK_URL || null;
if (!tgWebhookUrl) {
throw new ConfigError("Missing env var TELEGRAM_WEBHOOK_URL");
@@ -50,7 +53,8 @@ const googleSaJsonPath = path.join("sa_key.json");
fs.writeFileSync(googleSaJsonPath, googleSaJsonUtf8);
const config = {
- port: process.env.PORT ? Number(process.env.PORT) : 3000,
+ nodeEnv,
+ port,
tgWebhookUrl,
tgBotToken,
sheetsSpreadsheetId,
diff --git a/src/index.ts b/src/index.ts
index 8bb5c69..773ae3c 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -44,6 +44,7 @@ const main = async () => {
const newDateStamps = sheets.updatedLastDoneDateStamps(entries);
await sheets.updateSheetLastDoneColumn(newDateStamps);
+
res.sendStatus(200);
} catch (err) {
console.error(err);
diff --git a/src/middleware.ts b/src/middleware.ts
index 8239121..46d6e00 100644
--- a/src/middleware.ts
+++ b/src/middleware.ts
@@ -4,6 +4,7 @@ import got from "got";
import jwt from "jsonwebtoken";
import { getPermittedUsers } from "./db";
+import config from "./config";
const tgAuth = async (ctx: Context, next: () => Promise<void>): Promise<void> => {
const userId = ctx.message?.from.id;
@@ -19,6 +20,11 @@ const tgAuth = async (ctx: Context, next: () => Promise<void>): Promise<void> =>
};
const verifyGoogleJWT = async (req: Request, res: Response, next: () => void): Promise<void> => {
+ if (config.nodeEnv !== "production") {
+ next();
+ return;
+ }
+
const authHeader = req.headers.authorization || null;
if (!authHeader) {
res.sendStatus(401);
diff --git a/src/sheets.ts b/src/sheets.ts
index e99f089..d3d97e5 100644
--- a/src/sheets.ts
+++ b/src/sheets.ts
@@ -1,5 +1,5 @@
import { google } from "googleapis";
-import { add, Duration, parse, format, getDay, subDays, addDays } from "date-fns";
+import { add, set, Duration, parse, format, getDay, subDays, addDays } from "date-fns";
import config from "./config";
@@ -88,10 +88,12 @@ const processRow = (row: SheetsRawEntry) => {
const isThisWeek = (entry: SheetsProcessedEntry): boolean => {
const nextDate = entry.nextDate;
- const today = new Date();
+ const currentDateTime = new Date();
+ const today = set(currentDateTime, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 });
const dayOfWeek = (getDay(today) + 7 - 1) % 7; // getDay returns sunday = 0
const currentWeekStart = subDays(today, dayOfWeek);
const currentWeekEnd = addDays(currentWeekStart, 7);
+
return nextDate >= currentWeekStart && nextDate < currentWeekEnd;
};