2024-03-20 00:37:31 +00:00
|
|
|
import Login from '@/components/login'
|
2023-07-29 19:38:20 +00:00
|
|
|
import { getProviders } from 'next-auth/react'
|
|
|
|
import { getServerSession } from 'next-auth/next'
|
2024-03-20 00:37:31 +00:00
|
|
|
import models from '@/api/models'
|
|
|
|
import serialize from '@/api/resolvers/serial'
|
2021-10-15 23:07:51 +00:00
|
|
|
import { gql } from '@apollo/client'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { INVITE_FIELDS } from '@/fragments/invites'
|
|
|
|
import getSSRApolloClient from '@/api/ssrApollo'
|
2021-10-15 23:07:51 +00:00
|
|
|
import Link from 'next/link'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { CenterLayout } from '@/components/layout'
|
2024-03-20 18:41:32 +00:00
|
|
|
import { getAuthOptions } from '@/pages/api/auth/[...nextauth]'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { notifyInvite } from '@/lib/webPush'
|
2021-10-15 18:05:34 +00:00
|
|
|
|
|
|
|
export async function getServerSideProps ({ req, res, query: { id, error = null } }) {
|
2023-07-31 14:10:45 +00:00
|
|
|
const session = await getServerSession(req, res, getAuthOptions(req))
|
2021-10-15 18:05:34 +00:00
|
|
|
|
2023-07-29 19:38:20 +00:00
|
|
|
const client = await getSSRApolloClient({ req, res })
|
2021-10-15 23:07:51 +00:00
|
|
|
const { data } = await client.query({
|
|
|
|
query: gql`
|
|
|
|
${INVITE_FIELDS}
|
|
|
|
{
|
|
|
|
invite(id: "${id}") {
|
|
|
|
...InviteFields
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!data?.invite) {
|
|
|
|
return {
|
|
|
|
notFound: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 18:05:34 +00:00
|
|
|
if (session && res) {
|
2021-10-15 23:07:51 +00:00
|
|
|
try {
|
|
|
|
// attempt to send gift
|
|
|
|
// catch any errors and just ignore them for now
|
|
|
|
await serialize(models,
|
2023-07-27 00:18:42 +00:00
|
|
|
models.$queryRawUnsafe('SELECT invite_drain($1::INTEGER, $2::INTEGER)', session.user.id, id))
|
2023-10-04 23:20:52 +00:00
|
|
|
const invite = await models.invite.findUnique({ where: { id } })
|
2024-03-20 00:37:31 +00:00
|
|
|
notifyInvite(invite.userId)
|
2021-10-15 23:07:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
|
2023-07-29 19:38:20 +00:00
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
destination: '/',
|
|
|
|
permanent: false
|
|
|
|
}
|
|
|
|
}
|
2021-10-15 18:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
2023-07-29 19:38:20 +00:00
|
|
|
providers: await getProviders(),
|
2021-11-11 17:38:41 +00:00
|
|
|
callbackUrl: process.env.PUBLIC_URL + req.url,
|
2021-10-15 23:07:51 +00:00
|
|
|
invite: data.invite,
|
2021-10-15 18:05:34 +00:00
|
|
|
error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 23:07:51 +00:00
|
|
|
function InviteHeader ({ invite }) {
|
|
|
|
let Inner
|
|
|
|
if (invite.revoked) {
|
|
|
|
Inner = () => <div className='text-danger'>this invite link expired</div>
|
|
|
|
} else if ((invite.limit && invite.limit <= invite.invitees.length) || invite.poor) {
|
|
|
|
Inner = () => <div className='text-danger'>this invite link has no more sats</div>
|
|
|
|
} else {
|
|
|
|
Inner = () => (
|
|
|
|
<div>
|
2023-01-10 00:33:44 +00:00
|
|
|
Get <span className='text-success'>{invite.gift} free sats</span> from{' '}
|
2023-07-23 15:08:43 +00:00
|
|
|
<Link href={`/${invite.user.name}`}>@{invite.user.name}</Link>{' '}
|
2021-10-15 23:07:51 +00:00
|
|
|
when you sign up today
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-01-10 00:33:44 +00:00
|
|
|
<h3 className='text-center pb-3'>
|
2021-10-15 23:07:51 +00:00
|
|
|
<Inner />
|
2023-01-10 00:33:44 +00:00
|
|
|
</h3>
|
2021-10-15 23:07:51 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Invite ({ invite, ...props }) {
|
2023-01-10 23:13:37 +00:00
|
|
|
return (
|
2023-07-23 15:08:43 +00:00
|
|
|
<CenterLayout>
|
2023-01-10 23:13:37 +00:00
|
|
|
<Login Header={() => <InviteHeader invite={invite} />} text='Sign up' {...props} />
|
2023-07-23 15:08:43 +00:00
|
|
|
</CenterLayout>
|
2023-01-10 23:13:37 +00:00
|
|
|
)
|
2021-10-15 23:07:51 +00:00
|
|
|
}
|