Skip to content

Instantly share code, notes, and snippets.

@sostenesapollo
Created October 6, 2024 19:58
Show Gist options
  • Save sostenesapollo/91eed78f4686ae415b744bc5ec1b9a71 to your computer and use it in GitHub Desktop.
Save sostenesapollo/91eed78f4686ae415b744bc5ec1b9a71 to your computer and use it in GitHub Desktop.
import { createCheckoutSession } from "@/stripe.server";
import { ActionFunctionArgs, json, redirect } from "@remix-run/node";
import { nanoid } from "nanoid";
import { createClient } from "@/supabase.server";
import { sendMail } from "./mail.$name/send-mail";
export const action = async ({ request }: ActionFunctionArgs) => {
const body = await request.json();
// USD - Price as default
let price = body.fileUrls.length <= 5 ? "price_1Q4S3bRweDB86tNY1D0Mvknc" :
body.fileUrls.length <= 12 ? "price_1Q4S4BRweDB86tNYLpqX52rU" : body.fileUrls.length === "price_1Q4S4eRweDB86tNYFhqmoyVQ"
if(body?.location?.location?.country === 'BR') {
// 29,90 - 49,90 - 69,90
// price = body.fileUrls.length <= 5 ? "price_1Q1IbLRweDB86tNYQT3rKjUV" :
// body.fileUrls.length <= 12 ? "price_1Q2yLgRweDB86tNYowqbJ2FP" : "price_1Q2ygqRweDB86tNYtIx3t14T"
// 9,90 - 19,90 - 25,90
price = body.fileUrls.length <= 5 ? "price_1Q3HqfRweDB86tNYaKa4K8a3" :
body.fileUrls.length <= 12 ? "price_1Q3HrSRweDB86tNYAxIAmbwF" : "price_1Q3HrpRweDB86tNYKpZeJsad"
}
const { CHECKOUT_URL, CHECKOUT_SESSION_ID } = await createCheckoutSession(price);
if (!CHECKOUT_SESSION_ID) return json({ error: "Something went wrong" }, { status: 500 });
// The idea here is just to insert the uploaded files url in the db also the form data with the infos.
await insertFilesUrlsInDatabase(body, request, CHECKOUT_SESSION_ID);
// Stripe Url
return { CHECKOUT_URL };
};
const insertFilesUrlsInDatabase = async (body: any, request: any, CHECKOUT_SESSION_ID: any) => {
const supabase = createClient(request);
const dbRow = {
url_name: sanitizeString(body.name+nanoid().substring(0, 4)),
form_data : {
name: body.name,
start_date: body.start_date,
start_time: body.start_time,
message: body.message,
song_link: body.song_link,
filesUrls: body.fileUrls,
email: body.email,
location: body.location
},
session_id_stripe: CHECKOUT_SESSION_ID,
}
await supabase.from('users').insert(dbRow);
try {
await sendMail(dbRow);
}catch(e){
console.error('Error sending email:', e);
}
}
export const sanitizeString = (str) => str.normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/[^a-zA-Z0-9]/g, '-').toLowerCase();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment