mirror of
https://github.com/jjbrunton/Sauci.git
synced 2026-10-31 18:19:43 +00:00
Add question types, nudge feature, and share modal
- Add new question card components (photo, audio, text, who-likely) - Add share preview modal for matches - Add nudge notification system with rate limiting - Add session-based notification improvements - Add match confetti celebration animation - Update onboarding flow with paywall tracking - Remove deprecated swipe card and tutorial components - Update GlassCard/GlassInput to use solid backgrounds - Fix routing issues after packs screen removal
This commit is contained in:
1 parent
8612b16b93
commit
1daf386765
114 files changed
+14877
-7911
No files matched your search
@@ -6,14 +6,22 @@ const corsHeaders = {
|
||||
"Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type",
|
||||
};
|
||||
|
||||
type Answer = "yes" | "no" | "maybe";
|
||||
type MatchType = "yes_yes" | "yes_maybe" | "maybe_maybe";
|
||||
|
||||
interface UpdateResponseBody {
|
||||
question_id: string;
|
||||
new_answer: Answer;
|
||||
confirm_delete_match?: boolean;
|
||||
}
|
||||
type Answer = "yes" | "no" | "maybe";
|
||||
type MatchType = "yes_yes" | "yes_maybe" | "maybe_maybe" | "both_answered";
|
||||
type QuestionType = "swipe" | "text_answer" | "audio" | "photo" | "who_likely";
|
||||
|
||||
type ResponseData =
|
||||
| { type: "text_answer"; text: string }
|
||||
| { type: "audio"; media_path: string; duration_seconds: number }
|
||||
| { type: "photo"; media_path: string }
|
||||
| { type: "who_likely"; chosen_user_id: string };
|
||||
|
||||
interface UpdateResponseBody {
|
||||
question_id: string;
|
||||
new_answer: Answer;
|
||||
response_data?: ResponseData | null;
|
||||
confirm_delete_match?: boolean;
|
||||
}
|
||||
|
||||
interface UpdateResponseResult {
|
||||
success: boolean;
|
||||
@@ -21,16 +29,34 @@ interface UpdateResponseResult {
|
||||
match_id?: string;
|
||||
message_count?: number;
|
||||
new_match?: Record<string, unknown> | null;
|
||||
match_deleted?: boolean;
|
||||
match_type_updated?: boolean;
|
||||
}
|
||||
|
||||
function calculateMatch(a1: Answer, a2: Answer): MatchType | null {
|
||||
if (a1 === "no" || a2 === "no") return null;
|
||||
if (a1 === "yes" && a2 === "yes") return "yes_yes";
|
||||
if (a1 === "maybe" && a2 === "maybe") return "maybe_maybe";
|
||||
return "yes_maybe";
|
||||
}
|
||||
match_deleted?: boolean;
|
||||
match_type_updated?: boolean;
|
||||
}
|
||||
|
||||
interface Question {
|
||||
id: string;
|
||||
question_type?: QuestionType;
|
||||
}
|
||||
|
||||
function calculateMatchType(question: Question, answer1: Answer, answer2: Answer): MatchType | null {
|
||||
if (!question.question_type || question.question_type === "swipe") {
|
||||
if (answer1 === "no" || answer2 === "no") return null;
|
||||
if (answer1 === "yes" && answer2 === "yes") return "yes_yes";
|
||||
if (answer1 === "maybe" && answer2 === "maybe") return "maybe_maybe";
|
||||
return "yes_maybe";
|
||||
}
|
||||
|
||||
if (["text_answer", "audio", "photo"].includes(question.question_type)) {
|
||||
if (answer1 === "no" || answer2 === "no") return null;
|
||||
return "both_answered";
|
||||
}
|
||||
|
||||
if (question.question_type === "who_likely") {
|
||||
return "both_answered";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
// Handle CORS preflight
|
||||
@@ -65,7 +91,7 @@ Deno.serve(async (req) => {
|
||||
}
|
||||
|
||||
// Parse request body
|
||||
const { question_id, new_answer, confirm_delete_match }: UpdateResponseBody = await req.json();
|
||||
const { question_id, new_answer, response_data, confirm_delete_match }: UpdateResponseBody = await req.json();
|
||||
|
||||
if (!question_id || !new_answer) {
|
||||
return new Response(
|
||||
@@ -96,13 +122,27 @@ Deno.serve(async (req) => {
|
||||
);
|
||||
}
|
||||
|
||||
// Get current response
|
||||
const { data: currentResponse, error: currentResponseError } = await supabase
|
||||
.from("responses")
|
||||
.select("answer")
|
||||
.eq("user_id", user.id)
|
||||
.eq("question_id", question_id)
|
||||
.maybeSingle();
|
||||
// Fetch the question to get its type
|
||||
const { data: question, error: questionError } = await supabase
|
||||
.from("questions")
|
||||
.select("id, question_type")
|
||||
.eq("id", question_id)
|
||||
.single();
|
||||
|
||||
if (questionError || !question) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: "Question not found" }),
|
||||
{ status: 404, headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
// Get current response
|
||||
const { data: currentResponse, error: currentResponseError } = await supabase
|
||||
.from("responses")
|
||||
.select("answer, response_data")
|
||||
.eq("user_id", user.id)
|
||||
.eq("question_id", question_id)
|
||||
.maybeSingle();
|
||||
|
||||
if (currentResponseError) {
|
||||
return new Response(
|
||||
@@ -118,13 +158,13 @@ Deno.serve(async (req) => {
|
||||
);
|
||||
}
|
||||
|
||||
// If answer hasn't changed, return early
|
||||
if (currentResponse.answer === new_answer) {
|
||||
return new Response(
|
||||
JSON.stringify({ success: true, message: "Answer unchanged" } as UpdateResponseResult),
|
||||
{ headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
// If answer hasn't changed and no response_data updates, return early
|
||||
if (currentResponse.answer === new_answer && typeof response_data === "undefined") {
|
||||
return new Response(
|
||||
JSON.stringify({ success: true, message: "Answer unchanged" } as UpdateResponseResult),
|
||||
{ headers: { ...corsHeaders, "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if a match exists for this question
|
||||
const { data: existingMatch } = await supabase
|
||||
@@ -135,15 +175,28 @@ Deno.serve(async (req) => {
|
||||
.maybeSingle();
|
||||
|
||||
// Get partner's response
|
||||
const { data: partnerResponse } = await supabase
|
||||
.from("responses")
|
||||
.select("answer")
|
||||
.eq("couple_id", profile.couple_id)
|
||||
.eq("question_id", question_id)
|
||||
.neq("user_id", user.id)
|
||||
.maybeSingle();
|
||||
|
||||
const result: UpdateResponseResult = { success: true };
|
||||
const { data: partnerResponse } = await supabase
|
||||
.from("responses")
|
||||
.select("answer, response_data, user_id")
|
||||
.eq("couple_id", profile.couple_id)
|
||||
.eq("question_id", question_id)
|
||||
.neq("user_id", user.id)
|
||||
.maybeSingle();
|
||||
|
||||
const result: UpdateResponseResult = { success: true };
|
||||
const isNonSwipe = !!question.question_type && question.question_type !== "swipe";
|
||||
const normalizedResponseData = isNonSwipe && new_answer !== "no"
|
||||
? (typeof response_data !== "undefined" ? response_data : currentResponse.response_data ?? null)
|
||||
: null;
|
||||
const matchType = partnerResponse
|
||||
? calculateMatchType(question as Question, new_answer, partnerResponse.answer as Answer)
|
||||
: null;
|
||||
const responseSummary = isNonSwipe && matchType === "both_answered" && partnerResponse
|
||||
? {
|
||||
[user.id]: normalizedResponseData ?? null,
|
||||
[partnerResponse.user_id]: partnerResponse.response_data ?? null,
|
||||
}
|
||||
: null;
|
||||
|
||||
// Case 1: Changing to "no" AND match exists - requires confirmation
|
||||
if (new_answer === "no" && existingMatch) {
|
||||
@@ -195,63 +248,68 @@ Deno.serve(async (req) => {
|
||||
result.match_deleted = true;
|
||||
}
|
||||
|
||||
// Case 2: Changing FROM "no" to yes/maybe - may create a new match
|
||||
if (currentResponse.answer === "no" && new_answer !== "no" && partnerResponse) {
|
||||
const newMatchType = calculateMatch(new_answer, partnerResponse.answer as Answer);
|
||||
|
||||
if (newMatchType) {
|
||||
// Create new match
|
||||
const { data: newMatch, error: matchError } = await supabase
|
||||
.from("matches")
|
||||
.upsert(
|
||||
{
|
||||
couple_id: profile.couple_id,
|
||||
question_id,
|
||||
match_type: newMatchType,
|
||||
is_new: true,
|
||||
},
|
||||
{ onConflict: "couple_id,question_id" }
|
||||
)
|
||||
.select(`*, question:questions(*)`)
|
||||
.single();
|
||||
|
||||
if (!matchError && newMatch) {
|
||||
result.new_match = newMatch;
|
||||
|
||||
// Case 2: Changing FROM "no" to yes/maybe - may create a new match
|
||||
if (currentResponse.answer === "no" && new_answer !== "no" && partnerResponse) {
|
||||
if (matchType) {
|
||||
// Create new match
|
||||
const { data: newMatch, error: matchError } = await supabase
|
||||
.from("matches")
|
||||
.upsert(
|
||||
{
|
||||
couple_id: profile.couple_id,
|
||||
question_id,
|
||||
match_type: matchType,
|
||||
is_new: true,
|
||||
response_summary: responseSummary,
|
||||
},
|
||||
{ onConflict: "couple_id,question_id" }
|
||||
)
|
||||
.select(`*, question:questions(*)`)
|
||||
.single();
|
||||
|
||||
if (!matchError && newMatch) {
|
||||
result.new_match = newMatch;
|
||||
|
||||
// Match push notifications are queued via a Postgres trigger
|
||||
// (pending_match_notifications) to allow short digesting.
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Case 3: Changing between yes/maybe - may update match type
|
||||
if (
|
||||
currentResponse.answer !== "no" &&
|
||||
new_answer !== "no" &&
|
||||
existingMatch &&
|
||||
partnerResponse
|
||||
) {
|
||||
const newMatchType = calculateMatch(new_answer, partnerResponse.answer as Answer);
|
||||
|
||||
if (newMatchType) {
|
||||
const { error: updateMatchError } = await supabase
|
||||
.from("matches")
|
||||
.update({ match_type: newMatchType })
|
||||
.eq("id", existingMatch.id);
|
||||
|
||||
if (!updateMatchError) {
|
||||
result.match_type_updated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update the response
|
||||
const { error: updateError } = await supabase
|
||||
.from("responses")
|
||||
.update({ answer: new_answer })
|
||||
.eq("user_id", user.id)
|
||||
.eq("question_id", question_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Case 3: Changing between yes/maybe - may update match type
|
||||
if (
|
||||
new_answer !== "no" &&
|
||||
existingMatch &&
|
||||
partnerResponse &&
|
||||
matchType
|
||||
) {
|
||||
const updatePayload: Record<string, unknown> = { match_type: matchType };
|
||||
if (isNonSwipe) {
|
||||
updatePayload.response_summary = responseSummary;
|
||||
}
|
||||
|
||||
const { error: updateMatchError } = await supabase
|
||||
.from("matches")
|
||||
.update(updatePayload)
|
||||
.eq("id", existingMatch.id);
|
||||
|
||||
if (!updateMatchError) {
|
||||
result.match_type_updated = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the response
|
||||
const responseUpdate: Record<string, unknown> = { answer: new_answer };
|
||||
if (isNonSwipe || new_answer === "no" || typeof response_data !== "undefined") {
|
||||
responseUpdate.response_data = normalizedResponseData;
|
||||
}
|
||||
|
||||
const { error: updateError } = await supabase
|
||||
.from("responses")
|
||||
.update(responseUpdate)
|
||||
.eq("user_id", user.id)
|
||||
.eq("question_id", question_id);
|
||||
|
||||
if (updateError) {
|
||||
return new Response(
|
||||
|
||||
Reference in new issue
Block a user