stacker.news/api/resolvers/invite.js

67 lines
1.8 KiB
JavaScript
Raw Normal View History

import { GraphQLError } from 'graphql'
2023-02-08 19:38:04 +00:00
import { inviteSchema, ssValidate } from '../../lib/validate'
2023-07-27 00:18:42 +00:00
import { msatsToSats } from '../../lib/format'
2021-10-12 23:49:04 +00:00
export default {
Query: {
invites: async (parent, args, { me, models }) => {
if (!me) {
throw new GraphQLError('you must be logged in', { extensions: { code: 'FORBIDDEN' } })
2021-10-12 23:49:04 +00:00
}
return await models.invite.findMany({
where: {
userId: me.id
2021-10-15 23:07:51 +00:00
},
orderBy: {
createdAt: 'desc'
}
})
},
invite: async (parent, { id }, { me, models }) => {
return await models.invite.findUnique({
where: {
id
2021-10-12 23:49:04 +00:00
}
})
}
},
Mutation: {
createInvite: async (parent, { gift, limit }, { me, models }) => {
if (!me) {
throw new GraphQLError('you must be logged in', { extensions: { code: 'FORBIDDEN' } })
2021-10-12 23:49:04 +00:00
}
2023-02-08 19:38:04 +00:00
await ssValidate(inviteSchema, { gift, limit })
2021-10-12 23:49:04 +00:00
return await models.invite.create({
data: { gift, limit, userId: me.id }
})
2021-10-14 21:05:37 +00:00
},
revokeInvite: async (parent, { id }, { me, models }) => {
if (!me) {
throw new GraphQLError('you must be logged in', { extensions: { code: 'FORBIDDEN' } })
2021-10-14 21:05:37 +00:00
}
return await models.invite.update({
where: { id },
data: { revoked: true }
})
2021-10-12 23:49:04 +00:00
}
},
Invite: {
2021-10-14 21:05:37 +00:00
invitees: async (invite, args, { me, models }) => {
return await models.user.findMany({ where: { inviteId: invite.id } })
2021-10-15 23:07:51 +00:00
},
user: async (invite, args, { me, models }) => {
return await models.user.findUnique({ where: { id: invite.userId } })
},
poor: async (invite, args, { me, models }) => {
const user = await models.user.findUnique({ where: { id: invite.userId } })
2023-07-27 00:18:42 +00:00
return msatsToSats(user.msats) < invite.gift
2021-10-14 21:05:37 +00:00
}
2021-10-12 23:49:04 +00:00
}
}