import Login from '../../components/login' import { getProviders } from 'next-auth/react' import { getServerSession } from 'next-auth/next' import models from '../../api/models' import serialize from '../../api/resolvers/serial' import { gql } from '@apollo/client' import { INVITE_FIELDS } from '../../fragments/invites' import getSSRApolloClient from '../../api/ssrApollo' import Link from 'next/link' import { CenterLayout } from '../../components/layout' import { getAuthOptions } from '../api/auth/[...nextauth]' import { sendUserNotification } from '../../api/webPush' export async function getServerSideProps ({ req, res, query: { id, error = null } }) { const session = await getServerSession(req, res, getAuthOptions(req)) const client = await getSSRApolloClient({ req, res }) const { data } = await client.query({ query: gql` ${INVITE_FIELDS} { invite(id: "${id}") { ...InviteFields } }` }) if (!data?.invite) { return { notFound: true } } if (session && res) { try { // attempt to send gift // catch any errors and just ignore them for now await serialize(models, models.$queryRawUnsafe('SELECT invite_drain($1::INTEGER, $2::INTEGER)', session.user.id, id)) const invite = await models.invite.findUnique({ where: { id } }) sendUserNotification(invite.userId, { title: 'your invite has been redeemed', tag: 'INVITE' }).catch(console.error) } catch (e) { console.log(e) } return { redirect: { destination: '/', permanent: false } } } return { props: { providers: await getProviders(), callbackUrl: process.env.PUBLIC_URL + req.url, invite: data.invite, error } } } function InviteHeader ({ invite }) { let Inner if (invite.revoked) { Inner = () =>
this invite link expired
} else if ((invite.limit && invite.limit <= invite.invitees.length) || invite.poor) { Inner = () =>
this invite link has no more sats
} else { Inner = () => (
Get {invite.gift} free sats from{' '} @{invite.user.name}{' '} when you sign up today
) } return (

) } export default function Invite ({ invite, ...props }) { return ( } text='Sign up' {...props} /> ) }