mirror of
https://github.com/jjbrunton/Sauci.git
synced 2026-11-01 18:19:43 +00:00
Add admin user details, pack notifications, remove crypto
- Add enabled packs and feature interests to admin user page - Add clickable links from responses to packs in admin - Add pack change notification edge function - Remove react-native-quick-crypto (unused E2EE) - Bump mobile version to 1.0.1 (versionCode 22) - Add EAS config and preview build scripts - Gitignore keystore and APK files Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a7a13c9614
commit
e671ae9d1c
18 files changed
+527
-188
No files matched your search
@@ -0,0 +1,142 @@
|
||||
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
|
||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
|
||||
|
||||
/**
|
||||
* Send delayed pack change notifications.
|
||||
* Called by pg_cron every 5 minutes to check for pending notifications
|
||||
* where notify_at has passed.
|
||||
*
|
||||
* Notifications are sent when a user enables new question packs. The notification
|
||||
* is delayed by 30 minutes, with the timer resetting on each new pack enable.
|
||||
* This prevents spam when a user enables multiple packs in quick succession.
|
||||
*/
|
||||
|
||||
interface ExpoPushMessage {
|
||||
to: string;
|
||||
title: string;
|
||||
body: string;
|
||||
sound?: string;
|
||||
data?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
async function sendExpoPushNotifications(messages: ExpoPushMessage[]) {
|
||||
if (messages.length === 0) return;
|
||||
|
||||
const response = await fetch("https://exp.host/--/api/v2/push/send", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
},
|
||||
body: JSON.stringify(messages),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.text();
|
||||
console.error("Expo push error:", error);
|
||||
throw new Error(`Failed to send push notification: ${error}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
// Only allow POST (from cron) or GET (for manual testing)
|
||||
if (req.method !== "POST" && req.method !== "GET") {
|
||||
return new Response("Method not allowed", { status: 405 });
|
||||
}
|
||||
|
||||
const supabase = createClient(
|
||||
Deno.env.get("SUPABASE_URL")!,
|
||||
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!
|
||||
);
|
||||
|
||||
try {
|
||||
// Get all pending notifications where notify_at has passed
|
||||
const { data: pendingNotifications, error: fetchError } = await supabase
|
||||
.from("pending_pack_notifications")
|
||||
.select("id, couple_id, changed_by_user_id, notify_at")
|
||||
.lte("notify_at", new Date().toISOString());
|
||||
|
||||
if (fetchError) {
|
||||
console.error("Error fetching pending notifications:", fetchError);
|
||||
return new Response(
|
||||
JSON.stringify({ error: fetchError.message }),
|
||||
{ status: 500, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
if (!pendingNotifications || pendingNotifications.length === 0) {
|
||||
return new Response(
|
||||
JSON.stringify({ sent: 0, message: "No pending notifications" }),
|
||||
{ headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
const messages: ExpoPushMessage[] = [];
|
||||
const processedIds: string[] = [];
|
||||
|
||||
for (const notification of pendingNotifications) {
|
||||
// Get the partner's push token (not the user who made changes)
|
||||
const { data: partner, error: partnerError } = await supabase
|
||||
.from("profiles")
|
||||
.select("push_token")
|
||||
.eq("couple_id", notification.couple_id)
|
||||
.neq("id", notification.changed_by_user_id)
|
||||
.maybeSingle();
|
||||
|
||||
if (partnerError) {
|
||||
console.error(
|
||||
`Error fetching partner for couple ${notification.couple_id}:`,
|
||||
partnerError
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (partner?.push_token) {
|
||||
messages.push({
|
||||
to: partner.push_token,
|
||||
title: "Question packs updated",
|
||||
body: "Your partner updated the question packs",
|
||||
sound: "default",
|
||||
data: { type: "pack_change" },
|
||||
});
|
||||
}
|
||||
|
||||
processedIds.push(notification.id);
|
||||
}
|
||||
|
||||
// Send all notifications
|
||||
if (messages.length > 0) {
|
||||
await sendExpoPushNotifications(messages);
|
||||
}
|
||||
|
||||
// Delete processed notifications
|
||||
if (processedIds.length > 0) {
|
||||
const { error: deleteError } = await supabase
|
||||
.from("pending_pack_notifications")
|
||||
.delete()
|
||||
.in("id", processedIds);
|
||||
|
||||
if (deleteError) {
|
||||
console.error("Error deleting processed notifications:", deleteError);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Sent ${messages.length} pack change notifications`);
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
sent: messages.length,
|
||||
processed: processedIds.length,
|
||||
}),
|
||||
{ headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("Unexpected error:", err);
|
||||
return new Response(
|
||||
JSON.stringify({ error: "Internal server error" }),
|
||||
{ status: 500, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
});
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,101 @@
|
||||
-- Add delayed pack change notification system
|
||||
-- Notifies partner when a user enables new question packs (with 30-min debounce)
|
||||
|
||||
-- Create pending notifications table
|
||||
CREATE TABLE pending_pack_notifications (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
couple_id UUID NOT NULL REFERENCES couples(id) ON DELETE CASCADE,
|
||||
changed_by_user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
notify_at TIMESTAMPTZ NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now(),
|
||||
UNIQUE(couple_id) -- Only one pending notification per couple (enables UPSERT for timer reset)
|
||||
);
|
||||
|
||||
-- Index for efficient cron queries
|
||||
CREATE INDEX idx_pending_pack_notifications_notify_at
|
||||
ON pending_pack_notifications(notify_at);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE pending_pack_notifications ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- RLS policies
|
||||
CREATE POLICY "Users can view their couple's pending notifications"
|
||||
ON pending_pack_notifications FOR SELECT
|
||||
USING (couple_id = get_auth_user_couple_id());
|
||||
|
||||
CREATE POLICY "Super admins can view all pending notifications"
|
||||
ON pending_pack_notifications FOR SELECT
|
||||
USING (is_super_admin());
|
||||
|
||||
-- Updated_at trigger
|
||||
CREATE TRIGGER update_pending_pack_notifications_updated_at
|
||||
BEFORE UPDATE ON pending_pack_notifications
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at();
|
||||
|
||||
-- Function to queue pack change notifications
|
||||
CREATE OR REPLACE FUNCTION queue_pack_change_notification()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
SET search_path = 'public'
|
||||
AS $$
|
||||
DECLARE
|
||||
v_user_id UUID;
|
||||
BEGIN
|
||||
-- Only trigger when enabling a pack (not disabling)
|
||||
IF NEW.enabled = false THEN
|
||||
RETURN NEW;
|
||||
END IF;
|
||||
|
||||
-- For UPDATE, only proceed if changing from disabled to enabled
|
||||
IF TG_OP = 'UPDATE' AND OLD.enabled = true THEN
|
||||
RETURN NEW;
|
||||
END IF;
|
||||
|
||||
-- Get the user making the change
|
||||
v_user_id := auth.uid();
|
||||
|
||||
-- Skip if no auth context (service role operations)
|
||||
IF v_user_id IS NULL THEN
|
||||
RETURN NEW;
|
||||
END IF;
|
||||
|
||||
-- UPSERT: Insert new notification or reset timer on existing one
|
||||
INSERT INTO pending_pack_notifications (
|
||||
couple_id,
|
||||
changed_by_user_id,
|
||||
notify_at
|
||||
)
|
||||
VALUES (
|
||||
NEW.couple_id,
|
||||
v_user_id,
|
||||
now() + interval '30 minutes'
|
||||
)
|
||||
ON CONFLICT (couple_id) DO UPDATE SET
|
||||
changed_by_user_id = EXCLUDED.changed_by_user_id,
|
||||
notify_at = now() + interval '30 minutes',
|
||||
updated_at = now();
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Create trigger on couple_packs
|
||||
CREATE TRIGGER on_couple_pack_enabled
|
||||
AFTER INSERT OR UPDATE OF enabled ON couple_packs
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION queue_pack_change_notification();
|
||||
|
||||
-- Schedule cron job (runs every 5 minutes)
|
||||
SELECT cron.schedule(
|
||||
'send-pack-change-notifications',
|
||||
'*/5 * * * *',
|
||||
$$
|
||||
SELECT net.http_post(
|
||||
url := 'https://ckjcrkjpmhqhiucifukx.supabase.co/functions/v1/send-pack-change-notification',
|
||||
headers := jsonb_build_object('Content-Type', 'application/json'),
|
||||
body := '{}'::jsonb
|
||||
)
|
||||
$$
|
||||
);
|
||||
Reference in new issue
Block a user