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

CPF-103 Update User model #111

Merged
merged 7 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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,43 @@
/*
Warnings:

- You are about to drop the column `certifiedById` on the `Subrecipient` table. All the data in the column will be lost.
- You are about to drop the column `invalidatedById` on the `UploadValidation` table. All the data in the column will be lost.
- You are about to drop the column `validatedById` on the `UploadValidation` table. All the data in the column will be lost.
- You are about to drop the column `organizationId` on the `User` table. All the data in the column will be lost.
- Made the column `name` on table `User` required. This step will fail if there are existing NULL values in that column.
- Made the column `agencyId` on table `User` required. This step will fail if there are existing NULL values in that column.
- Made the column `role` on table `User` required. This step will fail if there are existing NULL values in that column.

*/
-- DropForeignKey
ALTER TABLE "Subrecipient" DROP CONSTRAINT "Subrecipient_certifiedById_fkey";

-- DropForeignKey
ALTER TABLE "UploadValidation" DROP CONSTRAINT "UploadValidation_invalidatedById_fkey";

-- DropForeignKey
ALTER TABLE "UploadValidation" DROP CONSTRAINT "UploadValidation_validatedById_fkey";

-- DropForeignKey
ALTER TABLE "User" DROP CONSTRAINT "User_agencyId_fkey";

-- DropForeignKey
ALTER TABLE "User" DROP CONSTRAINT "User_organizationId_fkey";

-- AlterTable
ALTER TABLE "Subrecipient" DROP COLUMN "certifiedById";

-- AlterTable
ALTER TABLE "UploadValidation" DROP COLUMN "invalidatedById",
DROP COLUMN "validatedById";

-- AlterTable
ALTER TABLE "User" DROP COLUMN "organizationId",
ADD COLUMN "isActive" BOOLEAN NOT NULL DEFAULT true,
ALTER COLUMN "name" SET NOT NULL,
ALTER COLUMN "agencyId" SET NOT NULL,
ALTER COLUMN "role" SET NOT NULL;

-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_agencyId_fkey" FOREIGN KEY ("agencyId") REFERENCES "Agency"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
21 changes: 5 additions & 16 deletions api/db/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ model Agency {
model Organization {
id Int @id @default(autoincrement())
agencies Agency[]
users User[]
name String
reportingPeriods ReportingPeriod[]
uploads Upload[]
Expand All @@ -37,19 +36,15 @@ model Organization {
model User {
id Int @id @default(autoincrement())
email String
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Do we want to implement email validation when calling create/update user or is there a way to enforce it here? Also, do we want to use unique?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we would like to use unique here as well 👍 =1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ✅

name String?
agencyId Int?
organizationId Int?
name String
agencyId Int
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @default(now()) @db.Timestamptz(6)
role Role?
agency Agency? @relation(fields: [agencyId], references: [id])
organization Organization? @relation(fields: [organizationId], references: [id])
role Role
isActive Boolean @default(true)
agency Agency @relation(fields: [agencyId], references: [id])
certified ReportingPeriod[]
uploaded Upload[]
validated UploadValidation[] @relation("ValidatedUploads")
invalidated UploadValidation[] @relation("InvalidatedUploads")
subrecipients Subrecipient[]
}

enum Role {
Expand Down Expand Up @@ -143,12 +138,8 @@ model UploadValidation {
inputTemplate InputTemplate @relation(fields: [inputTemplateId], references: [id])
validationResults Json? @db.JsonB
validatedAt DateTime? @db.Timestamptz(6)
validatedById Int?
validatedBy User? @relation("ValidatedUploads", fields: [validatedById], references: [id])
invalidationResults Json? @db.JsonB
invalidatedAt DateTime? @db.Timestamptz(6)
invalidatedById Int?
invalidatedBy User? @relation("InvalidatedUploads", fields: [invalidatedById], references: [id])
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @default(now()) @db.Timestamptz(6)
}
Expand All @@ -161,8 +152,6 @@ model Subrecipient {
startDate DateTime @db.Date
endDate DateTime @db.Date
certifiedAt DateTime? @db.Timestamptz(6)
certifiedById Int?
certifiedBy User? @relation(fields: [certifiedById], references: [id])
originationUploadId Int
originationUpload Upload @relation(fields: [originationUploadId], references: [id])
createdAt DateTime @default(now()) @db.Timestamptz(6)
Expand Down
6 changes: 0 additions & 6 deletions api/src/graphql/users.sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ export const schema = gql`
email: String!
name: String
agencyId: Int
organizationId: Int!
createdAt: DateTime!
updatedAt: DateTime!
agency: Agency
organization: Organization!
role: RoleEnum
certified: [ReportingPeriod]!
uploaded: [Upload]!
validated: [UploadValidation]!
invalidated: [UploadValidation]!
}

type Query {
Expand All @@ -32,15 +28,13 @@ export const schema = gql`
email: String!
name: String
agencyId: Int
organizationId: Int
role: RoleEnum
}

input UpdateUserInput {
email: String
name: String
agencyId: Int
organizationId: Int
role: RoleEnum
}

Expand Down
8 changes: 6 additions & 2 deletions api/src/services/subrecipients/subrecipients.scenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ export const standard = defineScenario<Prisma.SubrecipientCreateArgs>({
updatedAt: '2023-12-09T14:50:18.317Z',
uploadedBy: {
create: {
name: 'String',
email: 'String',
updatedAt: '2023-12-09T14:50:18.317Z',
organization: { create: { name: 'String' } },
agency: { create: { name: 'String', code: 'String' } },
role: 'USDR_ADMIN',
},
},
agency: { create: { name: 'String', code: 'String' } },
Expand Down Expand Up @@ -73,9 +75,11 @@ export const standard = defineScenario<Prisma.SubrecipientCreateArgs>({
updatedAt: '2023-12-09T14:50:18.317Z',
uploadedBy: {
create: {
name: 'String',
email: 'String',
updatedAt: '2023-12-09T14:50:18.317Z',
organization: { create: { name: 'String' } },
agency: { create: { name: 'String', code: 'String' } },
role: 'USDR_ADMIN',
},
},
agency: { create: { name: 'String', code: 'String' } },
Expand Down
3 changes: 0 additions & 3 deletions api/src/services/subrecipients/subrecipients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ export const Subrecipient: SubrecipientRelationResolvers = {
.findUnique({ where: { id: root?.id } })
.organization()
},
certifiedBy: (_obj, { root }) => {
return db.subrecipient.findUnique({ where: { id: root?.id } }).certifiedBy()
},
originationUpload: (_obj, { root }) => {
return db.subrecipient
.findUnique({ where: { id: root?.id } })
Expand Down
18 changes: 16 additions & 2 deletions api/src/services/uploads/uploads.scenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ export const standard = defineScenario<Prisma.UploadCreateArgs>({
one: {
data: {
filename: 'String',
uploadedBy: { create: { email: 'String' } },
uploadedBy: {
create: {
email: 'String',
name: 'String',
role: 'USDR_ADMIN',
agency: { create: { name: 'String', code: 'String' } },
},
},
agency: { create: { name: 'String', code: 'String' } },
organization: { create: { name: 'String' } },
reportingPeriod: {
Expand Down Expand Up @@ -37,7 +44,14 @@ export const standard = defineScenario<Prisma.UploadCreateArgs>({
two: {
data: {
filename: 'String',
uploadedBy: { create: { email: 'String' } },
uploadedBy: {
create: {
email: 'String',
name: 'String',
role: 'USDR_ADMIN',
agency: { create: { name: 'String', code: 'String' } },
},
},
agency: { create: { name: 'String', code: 'String' } },
organization: { create: { name: 'String' } },
reportingPeriod: {
Expand Down
1 change: 0 additions & 1 deletion api/src/services/users/users.scenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export const standard = defineScenario<
data: {
email: 'String',
name: 'String',
organization: { create: { name: 'String' } },
agency: { create: { name: 'String', code: 'String' } },
role: 'ORGANIZATION_ADMIN',
},
Expand Down
2 changes: 0 additions & 2 deletions api/src/services/users/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe('users', () => {
scenario('creates a user', async (scenario: StandardScenario) => {
mockCurrentUser({
id: scenario.user.one.id,
organizationId: scenario.user.one.organizationId,
email: '[email protected]',
roles: ['USDR_ADMIN'],
})
Expand All @@ -40,7 +39,6 @@ describe('users', () => {
})

expect(result.email).toEqual(scenario.user.one.email)
expect(result.organizationId).toEqual(scenario.organization.one.id)
})

scenario('updates a user', async (scenario: StandardScenario) => {
Expand Down
11 changes: 1 addition & 10 deletions api/src/services/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const createUser: MutationResolvers['createUser'] = async ({
}

return db.user.create({
data: { ...input, organizationId: agency.organizationId },
data: input,
})
} catch (err) {
throw new Error(err)
Expand Down Expand Up @@ -101,19 +101,10 @@ export const User: UserRelationResolvers = {
agency: (_obj, { root }) => {
return db.user.findUnique({ where: { id: root?.id } }).agency()
},
organization: (_obj, { root }) => {
return db.user.findUnique({ where: { id: root?.id } }).organization()
},
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()
},
}
14 changes: 0 additions & 14 deletions api/types/graphql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ export type CreateUserInput = {
agencyId?: InputMaybe<Scalars['Int']>;
email: Scalars['String'];
name?: InputMaybe<Scalars['String']>;
organizationId?: InputMaybe<Scalars['Int']>;
role?: InputMaybe<RoleEnum>;
};

Expand Down Expand Up @@ -696,7 +695,6 @@ export type UpdateUserInput = {
agencyId?: InputMaybe<Scalars['Int']>;
email?: InputMaybe<Scalars['String']>;
name?: InputMaybe<Scalars['String']>;
organizationId?: InputMaybe<Scalars['Int']>;
role?: InputMaybe<RoleEnum>;
};

Expand Down Expand Up @@ -753,14 +751,10 @@ export type User = {
createdAt: Scalars['DateTime'];
email: Scalars['String'];
id: Scalars['Int'];
invalidated: Array<Maybe<UploadValidation>>;
name?: Maybe<Scalars['String']>;
organization: Organization;
organizationId: Scalars['Int'];
role?: Maybe<RoleEnum>;
updatedAt: Scalars['DateTime'];
uploaded: Array<Maybe<Upload>>;
validated: Array<Maybe<UploadValidation>>;
};

type MaybeOrArrayOfMaybe<T> = T | Maybe<T> | Maybe<T>[];
Expand Down Expand Up @@ -1439,14 +1433,10 @@ export type UserResolvers<ContextType = RedwoodGraphQLContext, ParentType extend
createdAt: OptArgsResolverFn<ResolversTypes['DateTime'], ParentType, ContextType>;
email: OptArgsResolverFn<ResolversTypes['String'], ParentType, ContextType>;
id: OptArgsResolverFn<ResolversTypes['Int'], ParentType, ContextType>;
invalidated: OptArgsResolverFn<Array<Maybe<ResolversTypes['UploadValidation']>>, ParentType, ContextType>;
name: OptArgsResolverFn<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
organization: OptArgsResolverFn<ResolversTypes['Organization'], ParentType, ContextType>;
organizationId: OptArgsResolverFn<ResolversTypes['Int'], ParentType, ContextType>;
role: OptArgsResolverFn<Maybe<ResolversTypes['RoleEnum']>, ParentType, ContextType>;
updatedAt: OptArgsResolverFn<ResolversTypes['DateTime'], ParentType, ContextType>;
uploaded: OptArgsResolverFn<Array<Maybe<ResolversTypes['Upload']>>, ParentType, ContextType>;
validated: OptArgsResolverFn<Array<Maybe<ResolversTypes['UploadValidation']>>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

Expand All @@ -1457,14 +1447,10 @@ export type UserRelationResolvers<ContextType = RedwoodGraphQLContext, ParentTyp
createdAt?: RequiredResolverFn<ResolversTypes['DateTime'], ParentType, ContextType>;
email?: RequiredResolverFn<ResolversTypes['String'], ParentType, ContextType>;
id?: RequiredResolverFn<ResolversTypes['Int'], ParentType, ContextType>;
invalidated?: RequiredResolverFn<Array<Maybe<ResolversTypes['UploadValidation']>>, ParentType, ContextType>;
name?: RequiredResolverFn<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
organization?: RequiredResolverFn<ResolversTypes['Organization'], ParentType, ContextType>;
organizationId?: RequiredResolverFn<ResolversTypes['Int'], ParentType, ContextType>;
role?: RequiredResolverFn<Maybe<ResolversTypes['RoleEnum']>, ParentType, ContextType>;
updatedAt?: RequiredResolverFn<ResolversTypes['DateTime'], ParentType, ContextType>;
uploaded?: RequiredResolverFn<Array<Maybe<ResolversTypes['Upload']>>, ParentType, ContextType>;
validated?: RequiredResolverFn<Array<Maybe<ResolversTypes['UploadValidation']>>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

Expand Down
40 changes: 40 additions & 0 deletions scripts/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,46 @@ export default async () => {
// Seeds automatically with `yarn rw prisma migrate dev` and `yarn rw prisma migrate reset`
//

const organization: Prisma.OrganizationCreateArgs['data'] =
await db.organization.create({
data: {
name: 'Example Organization',
},
})

const agency: Prisma.AgencyCreateArgs['data'] = await db.agency.create({
data: {
name: 'Example Agency',
code: 'EA',
organizationId: organization.id,
},
})

const users: Prisma.UserCreateManyInput[] = [
{
email: '[email protected]',
name: 'Organization Staff',
role: 'ORGANIZATION_STAFF',
agencyId: agency.id,
},
{
email: '[email protected]',
name: 'Organization Admin',
role: 'ORGANIZATION_ADMIN',
agencyId: agency.id,
},
{
email: '[email protected]',
name: 'USDR Admin',
role: 'USDR_ADMIN',
agencyId: agency.id,
},
]

await db.user.createMany({
data: users,
})

const inputTemplates: Prisma.InputTemplateCreateArgs['data'][] = [
{
name: 'Input Template 1',
Expand Down
2 changes: 0 additions & 2 deletions web/src/components/User/EditUserCell/EditUserCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const QUERY = gql`
email
name
agencyId
organizationId
role
createdAt
updatedAt
Expand All @@ -28,7 +27,6 @@ const UPDATE_USER_MUTATION = gql`
email
name
agencyId
organizationId
role
createdAt
updatedAt
Expand Down
1 change: 0 additions & 1 deletion web/src/components/User/UserCell/UserCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const QUERY = gql`
email
name
agencyId
organizationId
role
createdAt
updatedAt
Expand Down
1 change: 0 additions & 1 deletion web/src/components/User/UsersCell/UsersCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const QUERY = gql`
agency {
name
}
organizationId
role
createdAt
updatedAt
Expand Down
Loading
Loading