stacker.news/api/resolvers/invite.js

66 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-02-08 19:38:04 +00:00
import { AuthenticationError } from 'apollo-server-micro'
import { inviteSchema, ssValidate } from '../../lib/validate'
2021-10-12 23:49:04 +00:00
export default {
Query: {
invites: async (parent, args, { me, models }) => {
if (!me) {
throw new AuthenticationError('you must be logged in')
}
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 AuthenticationError('you must be logged in')
}
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 AuthenticationError('you must be logged in')
}
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 } })
return user.msats < invite.gift * 1000
2021-10-14 21:05:37 +00:00
}
2021-10-12 23:49:04 +00:00
}
}