Add task to update tablo name in stream
This commit is contained in:
parent
7eea2ea340
commit
40171dc878
9 changed files with 1188 additions and 1091 deletions
|
|
@ -13,7 +13,7 @@ R2_ACCOUNT_ID="9715fa14c5e5d1612301572cf1c6bbee"
|
||||||
R2_ACCESS_KEY_ID="caeb987bbcd601708a93c6aa562064ef"
|
R2_ACCESS_KEY_ID="caeb987bbcd601708a93c6aa562064ef"
|
||||||
R2_SECRET_ACCESS_KEY="42e455b25804687f7cff3d15be23c1f0f47ca742d7a41b6fa1a05a91041e0215"
|
R2_SECRET_ACCESS_KEY="42e455b25804687f7cff3d15be23c1f0f47ca742d7a41b6fa1a05a91041e0215"
|
||||||
|
|
||||||
SYNC_CALS_SECRET="hello"
|
TASKS_SECRET="hello"
|
||||||
|
|
||||||
EMAIL_USER="baptiste@xtablo.com"
|
EMAIL_USER="baptiste@xtablo.com"
|
||||||
EMAIL_CLIENT_ID="904332563417-e2n7pchtgnkrkp360baaebfeig55maig.apps.googleusercontent.com"
|
EMAIL_CLIENT_ID="904332563417-e2n7pchtgnkrkp360baaebfeig55maig.apps.googleusercontent.com"
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ R2_ACCOUNT_ID="9715fa14c5e5d1612301572cf1c6bbee"
|
||||||
R2_ACCESS_KEY_ID="caeb987bbcd601708a93c6aa562064ef"
|
R2_ACCESS_KEY_ID="caeb987bbcd601708a93c6aa562064ef"
|
||||||
R2_SECRET_ACCESS_KEY="42e455b25804687f7cff3d15be23c1f0f47ca742d7a41b6fa1a05a91041e0215"
|
R2_SECRET_ACCESS_KEY="42e455b25804687f7cff3d15be23c1f0f47ca742d7a41b6fa1a05a91041e0215"
|
||||||
|
|
||||||
SYNC_CALS_SECRET="gT3BAytmNwhe1wKmvgREBlWcqK0="
|
TASKS_SECRET="gT3BAytmNwhe1wKmvgREBlWcqK0="
|
||||||
|
|
||||||
EMAIL_USER="baptiste@xtablo.com"
|
EMAIL_USER="baptiste@xtablo.com"
|
||||||
EMAIL_CLIENT_ID="904332563417-e2n7pchtgnkrkp360baaebfeig55maig.apps.googleusercontent.com"
|
EMAIL_CLIENT_ID="904332563417-e2n7pchtgnkrkp360baaebfeig55maig.apps.googleusercontent.com"
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ export interface AppConfig {
|
||||||
R2_SECRET_ACCESS_KEY: string;
|
R2_SECRET_ACCESS_KEY: string;
|
||||||
CORS_ORIGIN: string;
|
CORS_ORIGIN: string;
|
||||||
LOG_LEVEL: "debug" | "info" | "warn" | "error";
|
LOG_LEVEL: "debug" | "info" | "warn" | "error";
|
||||||
SYNC_CALS_SECRET: string;
|
TASKS_SECRET: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateEnvVar(name: string, value: string | undefined): string {
|
function validateEnvVar(name: string, value: string | undefined): string {
|
||||||
|
|
@ -61,7 +61,7 @@ function createConfig(): AppConfig {
|
||||||
R2_ACCOUNT_ID: validateEnvVar("R2_ACCOUNT_ID", process.env.R2_ACCOUNT_ID),
|
R2_ACCOUNT_ID: validateEnvVar("R2_ACCOUNT_ID", process.env.R2_ACCOUNT_ID),
|
||||||
R2_ACCESS_KEY_ID: validateEnvVar("R2_ACCESS_KEY_ID", process.env.R2_ACCESS_KEY_ID),
|
R2_ACCESS_KEY_ID: validateEnvVar("R2_ACCESS_KEY_ID", process.env.R2_ACCESS_KEY_ID),
|
||||||
R2_SECRET_ACCESS_KEY: validateEnvVar("R2_SECRET_ACCESS_KEY", process.env.R2_SECRET_ACCESS_KEY),
|
R2_SECRET_ACCESS_KEY: validateEnvVar("R2_SECRET_ACCESS_KEY", process.env.R2_SECRET_ACCESS_KEY),
|
||||||
SYNC_CALS_SECRET: process.env.SYNC_CALS_SECRET || "",
|
TASKS_SECRET: process.env.TASKS_SECRET || "",
|
||||||
LOG_LEVEL: "info",
|
LOG_LEVEL: "info",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,8 +1,10 @@
|
||||||
import { S3Client } from "@aws-sdk/client-s3";
|
import { S3Client } from "@aws-sdk/client-s3";
|
||||||
import type { SupabaseClient } from "@supabase/supabase-js";
|
import type { SupabaseClient } from "@supabase/supabase-js";
|
||||||
import { Hono } from "hono";
|
import { Hono, type Context } from "hono";
|
||||||
import { config } from "./config.js";
|
import { config } from "./config.js";
|
||||||
import { writeCalendarFileToR2 } from "./helpers.js";
|
import { writeCalendarFileToR2 } from "./helpers.js";
|
||||||
|
import { streamChatMiddleware } from "./middleware.js";
|
||||||
|
import type { StreamChat } from "stream-chat";
|
||||||
|
|
||||||
export const taskRouter = new Hono<{
|
export const taskRouter = new Hono<{
|
||||||
Variables: { supabase: SupabaseClient };
|
Variables: { supabase: SupabaseClient };
|
||||||
|
|
@ -10,7 +12,7 @@ export const taskRouter = new Hono<{
|
||||||
|
|
||||||
taskRouter.post("/sync-calendars", async (c) => {
|
taskRouter.post("/sync-calendars", async (c) => {
|
||||||
const supabase = c.get("supabase");
|
const supabase = c.get("supabase");
|
||||||
if (c.req.header("Authorization") !== `Basic ${config.SYNC_CALS_SECRET}`) {
|
if (c.req.header("Authorization") !== `Basic ${config.TASKS_SECRET}`) {
|
||||||
return c.json({ error: "Unauthorized" }, 401);
|
return c.json({ error: "Unauthorized" }, 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,3 +50,45 @@ taskRouter.post("/sync-calendars", async (c) => {
|
||||||
|
|
||||||
return c.json({ message: "Synced calendars" });
|
return c.json({ message: "Synced calendars" });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
taskRouter.post(
|
||||||
|
"/sync-tablo-names",
|
||||||
|
streamChatMiddleware,
|
||||||
|
async (
|
||||||
|
c: Context<{ Variables: { supabase: SupabaseClient; streamServerClient: StreamChat } }>
|
||||||
|
) => {
|
||||||
|
const supabase = c.get("supabase");
|
||||||
|
const streamServerClient = c.get("streamServerClient");
|
||||||
|
|
||||||
|
if (c.req.header("Authorization") !== `Basic ${config.TASKS_SECRET}`) {
|
||||||
|
return c.json({ error: "Unauthorized" }, 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
const fifteenMinutesInMilliseconds = 1000 * 60 * 15;
|
||||||
|
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from("tablos")
|
||||||
|
.select("id, name")
|
||||||
|
.gt("updated_at", new Date(Date.now() - fifteenMinutesInMilliseconds).toISOString());
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return c.json({ error: error.message }, 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
const tablosData = data as { id: string; name: string }[];
|
||||||
|
|
||||||
|
tablosData.forEach(async (tablo) => {
|
||||||
|
const channel = streamServerClient.channel("messaging", tablo.id);
|
||||||
|
try {
|
||||||
|
await channel.update({
|
||||||
|
// @ts-ignore
|
||||||
|
name: tablo.name,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`error updating channel, tablo id: ${tablo.id}, error: ${error}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return c.json({ message: `Synced ${tablosData.length} tablo names` });
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load diff
28
sql/33_add_updated_at_column_to_tablos.sql
Normal file
28
sql/33_add_updated_at_column_to_tablos.sql
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
-- Add updated_at column to tablos table
|
||||||
|
ALTER TABLE tablos
|
||||||
|
ADD COLUMN IF NOT EXISTS updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
|
||||||
|
|
||||||
|
-- Update existing rows to have updated_at = created_at for consistency
|
||||||
|
UPDATE tablos
|
||||||
|
SET updated_at = created_at
|
||||||
|
WHERE updated_at IS NULL;
|
||||||
|
|
||||||
|
-- Create function to update updated_at timestamp for tablos
|
||||||
|
CREATE OR REPLACE FUNCTION update_tablos_updated_at()
|
||||||
|
RETURNS TRIGGER AS $$
|
||||||
|
BEGIN
|
||||||
|
NEW.updated_at = CURRENT_TIMESTAMP;
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ language 'plpgsql';
|
||||||
|
|
||||||
|
-- Create trigger to automatically update updated_at on tablos
|
||||||
|
CREATE TRIGGER update_tablos_updated_at
|
||||||
|
BEFORE UPDATE ON tablos
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION update_tablos_updated_at();
|
||||||
|
|
||||||
|
-- Add comment to document the column
|
||||||
|
COMMENT ON COLUMN tablos.updated_at IS
|
||||||
|
'Timestamp when the tablo was last updated (auto-updated by trigger)';
|
||||||
|
|
||||||
|
|
@ -535,6 +535,7 @@ export type Database = {
|
||||||
owner_id: string
|
owner_id: string
|
||||||
position: number
|
position: number
|
||||||
status: string
|
status: string
|
||||||
|
updated_at: string | null
|
||||||
}
|
}
|
||||||
Insert: {
|
Insert: {
|
||||||
color?: string | null
|
color?: string | null
|
||||||
|
|
@ -546,6 +547,7 @@ export type Database = {
|
||||||
owner_id: string
|
owner_id: string
|
||||||
position?: number
|
position?: number
|
||||||
status?: string
|
status?: string
|
||||||
|
updated_at?: string | null
|
||||||
}
|
}
|
||||||
Update: {
|
Update: {
|
||||||
color?: string | null
|
color?: string | null
|
||||||
|
|
@ -557,6 +559,7 @@ export type Database = {
|
||||||
owner_id?: string
|
owner_id?: string
|
||||||
position?: number
|
position?: number
|
||||||
status?: string
|
status?: string
|
||||||
|
updated_at?: string | null
|
||||||
}
|
}
|
||||||
Relationships: []
|
Relationships: []
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue