diff --git a/appsync.d.ts b/appsync.d.ts index d91b7db..85c4317 100644 --- a/appsync.d.ts +++ b/appsync.d.ts @@ -21,6 +21,13 @@ export type CharityProfile = { request?: Maybe; }; +export type CharityUser = { + __typename?: 'CharityUser'; + firstName: Scalars['String']['output']; + lastName: Scalars['String']['output']; + name: Scalars['String']['output']; +}; + export type JoinRequest = { __typename?: 'JoinRequest'; email: Scalars['String']['output']; @@ -162,6 +169,13 @@ export type SchoolProfile = { request?: Maybe; }; +export type SchoolUser = { + __typename?: 'SchoolUser'; + firstName: Scalars['String']['output']; + lastName: Scalars['String']['output']; + name: Scalars['String']['output']; +}; + export type SignUpData = { __typename?: 'SignUpData'; email: Scalars['String']['output']; diff --git a/schema.graphql b/schema.graphql index 542811f..2b17287 100644 --- a/schema.graphql +++ b/schema.graphql @@ -45,6 +45,18 @@ type LocalAuthorityUser { notes: String } +type SchoolUser { + name: String! + firstName: String! + lastName: String! +} + +type CharityUser { + name: String! + firstName: String! + lastName: String! +} + type LocalAuthority { code: String! name: String! diff --git a/src/repository/charityUserRepository.ts b/src/repository/charityUserRepository.ts new file mode 100644 index 0000000..62c3f07 --- /dev/null +++ b/src/repository/charityUserRepository.ts @@ -0,0 +1,62 @@ +import { Collection, Db, Filter, MongoClient, WithId } from 'mongodb'; +import { CharityUser } from '../../appsync'; + +export class CharityUserRepository { + private static instance: CharityUserRepository; + private readonly client: MongoClient; + private readonly db: Db; + private readonly collection: Collection; + + private constructor() { + this.client = new MongoClient( + process?.env?.MONGODB_CONNECTION_STRING ?? 'mongodb://localhost:27017/', + { + auth: { + username: process?.env?.MONGODB_ADMIN_USERNAME, + password: process?.env?.MONGODB_ADMIN_PASSWORD, + }, + } + ); + this.db = this.client.db('D2E'); + this.collection = this.db.collection('CharityUser'); + } + + static getInstance(): CharityUserRepository { + if (!this.instance) { + this.instance = new CharityUserRepository(); + } + return this.instance; + } + + private async getByQuery(query: Filter): Promise[]> { + const cursor = this.collection.find(query); + + if (!(await cursor.hasNext())) { + return []; + } + + return await cursor.toArray(); + } + + private async getOne(query: Filter): Promise | undefined> { + const result = await this.collection.findOne(query); + + if (!result) { + return undefined; + } + + return result; + } + + public async getByEmail(email: string): Promise | undefined> { + return await this.getOne({ email }); + } + + public async list(): Promise[]> { + return await this.getByQuery({}); + } + + public async insert(charity: CharityUser): Promise { + return (await this.collection.insertOne(charity)).acknowledged; + } +} diff --git a/src/repository/schoolUserRepository.ts b/src/repository/schoolUserRepository.ts new file mode 100644 index 0000000..473cc27 --- /dev/null +++ b/src/repository/schoolUserRepository.ts @@ -0,0 +1,62 @@ +import { Collection, Db, Filter, MongoClient, WithId } from 'mongodb'; +import { SchoolUser } from '../../appsync'; + +export class SchoolUserRepository { + private static instance: SchoolUserRepository; + private readonly client: MongoClient; + private readonly db: Db; + private readonly collection: Collection; + + private constructor() { + this.client = new MongoClient( + process?.env?.MONGODB_CONNECTION_STRING ?? 'mongodb://localhost:27017/', + { + auth: { + username: process?.env?.MONGODB_ADMIN_USERNAME, + password: process?.env?.MONGODB_ADMIN_PASSWORD, + }, + } + ); + this.db = this.client.db('D2E'); + this.collection = this.db.collection('SchoolUser'); + } + + static getInstance(): SchoolUserRepository { + if (!this.instance) { + this.instance = new SchoolUserRepository(); + } + return this.instance; + } + + private async getByQuery(query: Filter): Promise[]> { + const cursor = this.collection.find(query); + + if (!(await cursor.hasNext())) { + return []; + } + + return await cursor.toArray(); + } + + private async getOne(query: Filter): Promise | undefined> { + const result = await this.collection.findOne(query); + + if (!result) { + return undefined; + } + + return result; + } + + public async getByEmail(email: string): Promise | undefined> { + return await this.getOne({ email }); + } + + public async list(): Promise[]> { + return await this.getByQuery({}); + } + + public async insert(school: SchoolUser): Promise { + return (await this.collection.insertOne(school)).acknowledged; + } +}