diff --git a/api/src/graphql/users.sdl.js b/api/src/graphql/users.sdl.js new file mode 100644 index 0000000..c01828b --- /dev/null +++ b/api/src/graphql/users.sdl.js @@ -0,0 +1,40 @@ +export const schema = gql` + type User { + id: Int! + name: String + email: String! + hashedPassword: String! + salt: String! + resetToken: String + resetTokenExpiresAt: DateTime + } + + type Query { + users: [User!]! @requireAuth + user(id: Int!): User @requireAuth + } + + input CreateUserInput { + name: String + email: String! + hashedPassword: String! + salt: String! + resetToken: String + resetTokenExpiresAt: DateTime + } + + input UpdateUserInput { + name: String + email: String + hashedPassword: String + salt: String + resetToken: String + resetTokenExpiresAt: DateTime + } + + type Mutation { + createUser(input: CreateUserInput!): User! @requireAuth + updateUser(id: Int!, input: UpdateUserInput!): User! @requireAuth + deleteUser(id: Int!): User! @requireAuth + } +` diff --git a/api/src/services/users/users.js b/api/src/services/users/users.js new file mode 100644 index 0000000..26245f2 --- /dev/null +++ b/api/src/services/users/users.js @@ -0,0 +1,30 @@ +import { db } from 'src/lib/db' + +export const users = () => { + return db.user.findMany() +} + +export const user = ({ id }) => { + return db.user.findUnique({ + where: { id }, + }) +} + +export const createUser = ({ input }) => { + return db.user.create({ + data: input, + }) +} + +export const updateUser = ({ id, input }) => { + return db.user.update({ + data: input, + where: { id }, + }) +} + +export const deleteUser = ({ id }) => { + return db.user.delete({ + where: { id }, + }) +} diff --git a/api/src/services/users/users.scenarios.js b/api/src/services/users/users.scenarios.js new file mode 100644 index 0000000..d16b70d --- /dev/null +++ b/api/src/services/users/users.scenarios.js @@ -0,0 +1,19 @@ +export const standard = defineScenario({ + user: { + one: { + data: { + email: 'String5632270', + hashedPassword: 'String', + salt: 'String', + }, + }, + + two: { + data: { + email: 'String1221605', + hashedPassword: 'String', + salt: 'String', + }, + }, + }, +}) diff --git a/api/src/services/users/users.test.js b/api/src/services/users/users.test.js new file mode 100644 index 0000000..dc34afc --- /dev/null +++ b/api/src/services/users/users.test.js @@ -0,0 +1,52 @@ +import { users, user, createUser, updateUser, deleteUser } from './users' + +// Generated boilerplate tests do not account for all circumstances +// and can fail without adjustments, e.g. Float and DateTime types. +// Please refer to the RedwoodJS Testing Docs: +// https://redwoodjs.com/docs/testing#testing-services +// https://redwoodjs.com/docs/testing#jest-expect-type-considerations + +describe('users', () => { + scenario('returns all users', async (scenario) => { + const result = await users() + + expect(result.length).toEqual(Object.keys(scenario.user).length) + }) + + scenario('returns a single user', async (scenario) => { + const result = await user({ id: scenario.user.one.id }) + + expect(result).toEqual(scenario.user.one) + }) + + scenario('creates a user', async () => { + const result = await createUser({ + input: { + email: 'String4358842', + hashedPassword: 'String', + salt: 'String', + }, + }) + + expect(result.email).toEqual('String4358842') + expect(result.hashedPassword).toEqual('String') + expect(result.salt).toEqual('String') + }) + + scenario('updates a user', async (scenario) => { + const original = await user({ id: scenario.user.one.id }) + const result = await updateUser({ + id: original.id, + input: { email: 'String5861252' }, + }) + + expect(result.email).toEqual('String5861252') + }) + + scenario('deletes a user', async (scenario) => { + const original = await deleteUser({ id: scenario.user.one.id }) + const result = await user({ id: original.id }) + + expect(result).toEqual(null) + }) +})