Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14/View users page, add user page, edit user page #48

Merged
merged 19 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "User" ALTER COLUMN "createdAt" SET DATA TYPE TIMESTAMPTZ(6),
ALTER COLUMN "updatedAt" SET DATA TYPE TIMESTAMPTZ(6);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- DropForeignKey
ALTER TABLE "User" DROP CONSTRAINT "User_organizationId_fkey";

-- AlterTable
ALTER TABLE "User" ALTER COLUMN "organizationId" DROP NOT NULL;

-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE SET NULL ON UPDATE CASCADE;
8 changes: 4 additions & 4 deletions api/db/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ model User {
email String
name String?
agencyId Int?
organizationId Int
organizationId Int?
roleId Int?
createdAt DateTime @default(now())
updatedAt DateTime
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz(6)
agency Agency? @relation(fields: [agencyId], references: [id])
organization Organization @relation(fields: [organizationId], references: [id])
organization Organization? @relation(fields: [organizationId], references: [id])
role Role? @relation(fields: [roleId], references: [id])
certified ReportingPeriod[]
uploaded Upload[]
Expand Down
6 changes: 4 additions & 2 deletions api/src/graphql/users.sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,28 @@ export const schema = gql`
organization: Organization!
role: Role
certified: [ReportingPeriod]!
uploaded: [Upload]!
validated: [UploadValidation]!
invalidated: [UploadValidation]!
}

type Query {
users: [User!]! @requireAuth
usersByOrganization(organizationId: Int!): [User!]! @requireAuth
user(id: Int!): User @requireAuth
}

input CreateUserInput {
email: String!
name: String
agencyId: Int
organizationId: Int!
roleId: Int
}

input UpdateUserInput {
email: String
name: String
agencyId: Int
organizationId: Int
roleId: Int
}

Expand Down
4 changes: 2 additions & 2 deletions api/src/services/users/users.scenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export const standard = defineScenario<Prisma.UserCreateArgs>({
one: {
data: {
email: 'String',
updatedAt: '2023-12-07T18:20:20.679Z',
updatedAt: '2023-12-10T00:37:26.049Z',
organization: { create: { name: 'String' } },
},
},
two: {
data: {
email: 'String',
updatedAt: '2023-12-07T18:20:20.679Z',
updatedAt: '2023-12-10T00:37:26.049Z',
organization: { create: { name: 'String' } },
},
},
Expand Down
4 changes: 2 additions & 2 deletions api/src/services/users/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ describe('users', () => {
input: {
email: 'String',
organizationId: scenario.user.two.organizationId,
updatedAt: '2023-12-07T18:20:20.664Z',
updatedAt: '2023-12-10T00:37:26.029Z',
},
})

expect(result.email).toEqual('String')
expect(result.organizationId).toEqual(scenario.user.two.organizationId)
expect(result.updatedAt).toEqual(new Date('2023-12-07T18:20:20.664Z'))
expect(result.updatedAt).toEqual(new Date('2023-12-10T00:37:26.029Z'))
})

scenario('updates a user', async (scenario: StandardScenario) => {
Expand Down
23 changes: 23 additions & 0 deletions api/src/services/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ export const deleteUser: MutationResolvers['deleteUser'] = ({ id }) => {
})
}

export const usersByOrganization: QueryResolvers['usersByOrganization'] =
async ({ organizationId }) => {
try {
const users = await db.user.findMany({
where: { organizationId },
})
return users || [] // Return an empty array if null is received
} catch (error) {
console.error(error)
// Handle the error appropriately; maybe log it and return an empty array
return []
}
}

export const User: UserRelationResolvers = {
agency: (_obj, { root }) => {
return db.user.findUnique({ where: { id: root?.id } }).agency()
Expand All @@ -48,4 +62,13 @@ export const User: UserRelationResolvers = {
certified: (_obj, { root }) => {
return db.user.findUnique({ where: { id: root?.id } }).certified()
},
uploaded: (_obj, { root }) => {
return db.user.findUnique({ where: { id: root?.id } }).uploaded()
},
validated: (_obj, { root }) => {
return db.user.findUnique({ where: { id: root?.id } }).validated()
},
invalidated: (_obj, { root }) => {
return db.user.findUnique({ where: { id: root?.id } }).invalidated()
},
}
Loading
Loading