I was trying to do POST request to the api.referral.jsx file and it shows 404 error
api.referral.jsx
import { json } from “@remix-run/node”;> import { cors } from “remix-utils/cors”;> import prisma from “../db.server”;> import { authenticate } from “../shopify.server”;> export async function action({ request }) {> const { session } = authenticate.public.appProxy(request);> console.log(session);> const urlParams = new URL(request.url).searchParams;> const ref = urlParams.get(“ref”);> const { method } = request;> switch (method) {> case “POST”:> try {> if (!ref) {> return cors(> request,> json({ error: “Missing referral ID” }, { status: 400 }),> );> }> const affiliateLink = await prisma.affiliateLink.findFirst({> where: { uniqueId: ref },> include: { affiliate: true },> });> if (!affiliateLink) {> return cors(> request,> json({ error: “Invalid referral ID” }, { status: 404 }),> );> }> const currentDate = new Date();> const startOfDay = new Date(currentDate);> startOfDay.setHours(0, 0, 0, 0);> const endOfDay = new Date(currentDate);> endOfDay.setHours(23, 59, 59, 999);> let performance = await prisma.affiliatePerformance.findFirst({> where: {> affiliateId: affiliateLink.affiliateId,> startDate: { lte: currentDate },> endDate: { gte: currentDate },> },> });> if (performance) {> performance = await prisma.affiliatePerformance.update({> where: { id: performance.id },> data: {> totalClicks: { increment: 1 },> updatedAt: currentDate,> },> });> } else {> performance = await prisma.affiliatePerformance.create({> data: {> affiliateId: affiliateLink.affiliateId,> totalClicks: 1,> startDate: startOfDay,> endDate: endOfDay,> },> });> }> return cors(> request,> json({> success: true,> message: “Click recorded successfully”,> }),> );> } catch (error) {> console.error(“Error processing referral click:”, error);> return cors(> request,> json(> {> error: “Failed to process referral click”,> details: error.message,> },> { status: 500 },> ),> );> }> default:> return cors(> request,> json({ error: “Method not allowed” }, { status: 405 }),> );> }> }
Liquid File
document.addEventListener(‘DOMContentLoaded’, async () => {> const urlParams = new URLSearchParams(window.location.search);> const ref = urlParams.get(‘ref’);> > if (!ref) return;> > try {> const response = await fetch(
/apps/affiliate/?ref=${ref}, {> method: ‘POST’,> headers: { ‘Content-Type’: ‘application/json’ },> });> > if (!response.ok) {> throw new Error(Server responded with status ${response.status});> }> > const result = await response.json();> console.log(‘Referral logged successfully:’, result);> } catch (err) {> console.error(‘Error sending referral:’, err);> }> });> > > {% schema %}> {> “name”: “Earnetra Referral Block”,> “target”: “section”,> “settings”: > }> > {% endschema %}> >
