Skip to content

Commit

Permalink
Custom invite code and note
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Nov 25, 2024
1 parent b0207a2 commit 5f8db90
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 20 deletions.
30 changes: 22 additions & 8 deletions api/resolvers/invite.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { inviteSchema, validateSchema } from '@/lib/validate'
import { msatsToSats } from '@/lib/format'
import assertApiKeyNotPermitted from './apiKey'
import { GqlAuthenticationError } from '@/lib/error'
import { GqlAuthenticationError, GqlInputError } from '@/lib/error'
import { Prisma } from '@prisma/client'

export default {
Query: {
invites: async (parent, args, { me, models }) => {
if (!me) {
throw new GqlAuthenticationError()
}

return await models.invite.findMany({
where: {
userId: me.id
Expand All @@ -29,17 +29,31 @@ export default {
},

Mutation: {
createInvite: async (parent, { gift, limit }, { me, models }) => {
createInvite: async (parent, { id, gift, limit, description }, { me, models }) => {
if (!me) {
throw new GqlAuthenticationError()
}
assertApiKeyNotPermitted({ me })

await validateSchema(inviteSchema, { gift, limit })

return await models.invite.create({
data: { gift, limit, userId: me.id }
})
await validateSchema(inviteSchema, { id, gift, limit, description })
try {
return await models.invite.create({
data: {
id,
gift,
limit,
userId: me.id,
description
}
})
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
if (error.code === 'P2002' && error.meta.target.includes('id')) {
throw new GqlInputError('an invite with this code already exists')
}
}
throw error
}
},
revokeInvite: async (parent, { id }, { me, models }) => {
if (!me) {
Expand Down
3 changes: 2 additions & 1 deletion api/typeDefs/invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default gql`
}
extend type Mutation {
createInvite(gift: Int!, limit: Int): Invite
createInvite(id:String, gift: Int!, limit: Int, description: String): Invite
revokeInvite(id: ID!): Invite
}
Expand All @@ -20,5 +20,6 @@ export default gql`
user: User!
revoked: Boolean!
poor: Boolean!
description: String
}
`
1 change: 1 addition & 0 deletions components/invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function Invite ({ invite, active }) {
size='sm' type='text'
placeholder={`${process.env.NEXT_PUBLIC_URL}/invites/${invite.id}`} readOnly noForm
/>
{invite.description && <small className='text-muted'>{invite.description}</small>}
<div className={styles.other}>
<span>{invite.gift} sat gift</span>
<span> \ </span>
Expand Down
2 changes: 1 addition & 1 deletion components/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function Invitification ({ n }) {
<>
<NoteHeader color='secondary'>
your invite has been redeemed by
{numWithUnits(n.invite.invitees.length, {
{' ' + numWithUnits(n.invite.invitees.length, {
abbreviate: false,
unitSingular: 'stacker',
unitPlural: 'stackers'
Expand Down
1 change: 1 addition & 0 deletions fragments/invites.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ export const INVITE_FIELDS = gql`
...StreakFields
}
poor
description
}
`
4 changes: 3 additions & 1 deletion lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,9 @@ export const bioSchema = object({

export const inviteSchema = object({
gift: intValidator.positive('must be greater than 0').required('required'),
limit: intValidator.positive('must be positive')
limit: intValidator.positive('must be positive'),
description: string().trim().max(42, 'must be at most 42 characters'),
id: string().matches(/^[\w-]+$/, 'invalid id').min(4, 'must be at least 4 characters').max(21, 'must be at most 21 characters')
})

export const pushSubscriptionSchema = object({
Expand Down
4 changes: 2 additions & 2 deletions lib/webPush.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,9 @@ export const notifyReferral = async (userId) => {
}
}

export const notifyInvite = async (userId) => {
export const notifyInvite = async (userId, inviteId, inviteDescription) => {
try {
await sendUserNotification(userId, { title: 'your invite has been redeemed', tag: 'INVITE' })
await sendUserNotification(userId, { title: `your invite with code ${inviteId} has been redeemed${inviteDescription ? ` (${inviteDescription})` : ''}`, tag: 'INVITE' })
} catch (err) {
console.error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pages/invites/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function getServerSideProps ({ req, res, query: { id, error = null
{ models }
)
const invite = await models.invite.findUnique({ where: { id } })
notifyInvite(invite.userId)
notifyInvite(invite.userId, invite.id, invite.description)
} catch (e) {
console.log(e)
}
Expand Down
24 changes: 18 additions & 6 deletions pages/invites/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function InviteForm () {
const [createInvite] = useMutation(
gql`
${INVITE_FIELDS}
mutation createInvite($gift: Int!, $limit: Int) {
createInvite(gift: $gift, limit: $limit) {
mutation createInvite($id: String, $gift: Int!, $limit: Int, $description: String) {
createInvite(id:$id, gift: $gift, limit: $limit, description: $description) {
...InviteFields
}
}`, {
Expand All @@ -42,19 +42,28 @@ function InviteForm () {
return (
<Form
initial={{
id: undefined,
gift: 100,
limit: 1
limit: 1,
description: undefined
}}
schema={inviteSchema}
onSubmit={async ({ limit, gift }) => {
onSubmit={async ({ id, gift, limit, description }) => {
const { error } = await createInvite({
variables: {
gift: Number(gift), limit: limit ? Number(limit) : limit
id,
gift: Number(gift),
limit: limit ? Number(limit) : limit,
description
}
})
if (error) throw error
}}
>
<Input
label={<>invite code <small className='text-muted ms-2'>optional</small></>}
name='id'
/>
<Input
label='gift'
name='gift'
Expand All @@ -65,7 +74,10 @@ function InviteForm () {
label={<>invitee limit <small className='text-muted ms-2'>optional</small></>}
name='limit'
/>

<Input
label={<>note <small className='text-muted ms-2'>optional</small></>}
name='description'
/>
<SubmitButton variant='secondary'>create</SubmitButton>
</Form>
)
Expand Down
2 changes: 2 additions & 0 deletions prisma/migrations/20241125195641_custom_invites/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Invite" ADD COLUMN "description" TEXT;
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ model Invite {
user User @relation("Invites", fields: [userId], references: [id], onDelete: Cascade)
invitees User[]
description String?
@@index([createdAt], map: "Invite.created_at_index")
@@index([userId], map: "Invite.userId_index")
}
Expand Down

0 comments on commit 5f8db90

Please sign in to comment.