Skip to content

EdgeDB - Partner and Organization Queries #3105

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

Merged
merged 8 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions dbschema/migrations/00007-m15wwix.edgeql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion dbschema/organization.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module default {
}

acronym: str;
#address: str; #TODO - this needs figured out - needed on here and Partner?
#TODO - this needs figured out - needed on here and Partner?
address: str;
multi types: Organization::Type;
multi reach: Organization::Reach;

Expand Down
5 changes: 4 additions & 1 deletion dbschema/partner.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module default {
constraint regexp(r'^[A-Z]{3}$');
}

#address: str; #TODO - this needs figured out - needed on here and Organization?
#TODO - this needs figured out - needed on here and Organization?
address: str;
multi types: Partner::Type;
multi financialReportingTypes: Partnership::FinancialReportingType;

Expand All @@ -31,6 +32,8 @@ module default {
multi languagesOfConsulting: Language;
multi fieldRegions: FieldRegion;
multi countries: Location;

startDate: cal::local_date;
}
}

Expand Down
16 changes: 16 additions & 0 deletions dbschema/seeds/006.organizations.edgeql
Original file line number Diff line number Diff line change
Expand Up @@ -2,90 +2,105 @@ with
organizationsJson := to_json('[
{
"name": "Eriador Church",
"address": "123 Sesame Street, Dallas, TX 12345",
"acronym": "EC",
"types": ["Church", "Mission"],
"reach": ["Local", "National"]
},
{
"name": "Ered Luin Translation Syndicate",
"address": "456 Wall Street, New York, NY 12345",
"acronym": "ELTW",
"types": ["Parachurch", "Mission"],
"reach": ["National", "Regional"]
},
{
"name": "Rohan Linguistics",
"address": "1650 Main St., Atlanta, GA",
"acronym": "RL",
"types": ["Church"],
"reach": ["Local", "Regional"]
},
{
"name": "Rhun For Zero",
"address": "975 1st Street, Jacksonville, FL 98765",
"acronym": "RFZ",
"types": ["TranslationOrganization", "Mission"],
"reach": ["Global"]
},
{
"name": "Gondor Foundation",
"address": "4567 2nd Street, Jeffersonville, IN 47130",
"acronym": "GF",
"types": ["Church", "TranslationOrganization"],
"reach": ["Local"]
},
{
"name": "Ered Mithrim Group",
"address": "5577 Market St., Georgetown, KY 40200",
"acronym": "EMG",
"types": ["Church", "Parachurch"],
"reach": ["Local", "Global"]
},
{
"name": "The Rivendell Partnership",
"address": "1234 Highway 64 NW, Georgetown, IN 47122",
"acronym": "TRP",
"types": ["Church"],
"reach": ["Global", "Regional"]
},
{
"name": "Dwarvish/Elvish Alliance",
"address": "1234 Highway 64 NW, Georgetown, IN 47122",
"acronym": "DEA",
"types": ["Parachurch", "Mission"],
"reach": ["Global"]
},
{
"name": "The Buckland Organization",
"address": "22 Peachtree Drive, Windsor Mill, MD 21244",
"acronym": "BO",
"types": ["Alliance", "Mission", "Parachurch"],
"reach": ["Local", "Global", "Regional"]
},
{
"name": "Fellowship of Halfing Languages",
"address": "45 W. Southampton Dr., Eastpointe, MI 48021",
"acronym": "FAHL",
"types": ["Alliance"],
"reach": ["Local", "National"]
},
{
"name": "Rivers and Mountains Translation Group",
"address": "738 Trout Street, Hollywood, FL 33020",
"acronym": "RMTG",
"types": ["Church", "Mission"],
"reach": ["Local", "Regional"]
},
{
"name": "Hobbiton Ministry",
"address": "659 Cherry Hill Ave., Parkville, MD 21234",
"acronym": "HM",
"types": ["Church", "Mission"],
"reach": ["National", "Regional"]
},
{
"name": "Linguistics Seminary of Sutherland",
"address": "46 Thatcher St., Port Jefferson Station, NY 11776",
"acronym": "LSS",
"types": ["Church"],
"reach": ["National", "Regional", "Global"]
},
{
"name": "Heart and Minds Across Belegaer",
"address": "9766 W. Hawthorne Avenue, Strongsville, OH 44136",
"acronym": "HMAB",
"types": ["Church", "Mission"],
"reach": ["National", "Regional"]
},
{
"name": "The Gray Havens Initiative",
"address": "6 S. Shore Circle, Thornton, CO 80241",
"acronym": "GHI",
"types": ["Mission"],
"reach": ["National", "Regional", "Global"]
Expand All @@ -98,6 +113,7 @@ with
(insert Organization {
projectContext := (insert Project::Context),
name := <str>organization['name'],
address := <str>organization['address'],
acronym := <str>organization['acronym'],
types := <str>json_array_unpack(organization['types']),
reach := <str>json_array_unpack(organization['reach'])
Expand Down
1 change: 1 addition & 0 deletions dbschema/seeds/007.partners.edgeql
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ with
organization := organization,
projectContext := organization.projectContext,
name := organization.name,
address := organization.address,
pmcEntityCode := <str>partner['pmcEntityCode'],
types := <str>json_array_unpack(partner['types']),
financialReportingTypes := <str>json_array_unpack(partner['financialReportingTypes']),
Expand Down
2 changes: 1 addition & 1 deletion src/components/organization/dto/organization.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Organization extends Resource {
readonly acronym: SecuredStringNullable;

@Field()
readonly address: SecuredString;
readonly address: SecuredStringNullable;

@SensitivityField({
description:
Expand Down
21 changes: 21 additions & 0 deletions src/components/organization/organization.edgedb.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@nestjs/common';
import { PublicOf } from '~/common';
import { e, RepoFor } from '~/core/edgedb';
import { CreateOrganization, Organization } from './dto';
import { OrganizationRepository } from './organization.repository';

@Injectable()
export class OrganizationEdgeDBRepository
extends RepoFor(Organization, {
hydrate: (organization) => organization['*'],
omit: ['create'],
})
implements PublicOf<OrganizationRepository>
{
async create(input: CreateOrganization) {
return await this.defaults.create({
...input,
projectContext: e.insert(e.Project.Context, {}),
});
}
}
4 changes: 3 additions & 1 deletion src/components/organization/organization.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { forwardRef, Module } from '@nestjs/common';
import { splitDb } from '~/core';
import { AuthorizationModule } from '../authorization/authorization.module';
import { LocationModule } from '../location/location.module';
import { AddOrganizationReachMigration } from './migrations/add-reach.migration';
import { AddOrganizationTypeMigration } from './migrations/add-type.migration';
import { OrganizationEdgeDBRepository } from './organization.edgedb.repository';
import { OrganizationLoader } from './organization.loader';
import { OrganizationRepository } from './organization.repository';
import { OrganizationResolver } from './organization.resolver';
Expand All @@ -13,7 +15,7 @@ import { OrganizationService } from './organization.service';
providers: [
OrganizationResolver,
OrganizationService,
OrganizationRepository,
splitDb(OrganizationRepository, OrganizationEdgeDBRepository),
OrganizationLoader,
AddOrganizationReachMigration,
AddOrganizationTypeMigration,
Expand Down
38 changes: 27 additions & 11 deletions src/components/organization/organization.repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Injectable } from '@nestjs/common';
import { node, Query, relation } from 'cypher-query-builder';
import { ChangesOf } from '~/core/database/changes';
import { ID, Session, UnsecuredDto } from '../../common';
import { DtoRepository } from '../../core';
import {
DuplicateException,
ID,
ServerException,
Session,
UnsecuredDto,
} from '~/common';
import { DtoRepository } from '~/core/database';
import {
ACTIVE,
createNode,
Expand All @@ -15,7 +20,7 @@ import {
rankSens,
requestingUser,
sorting,
} from '../../core/database/query';
} from '~/core/database/query';
import {
CreateOrganization,
Organization,
Expand All @@ -28,7 +33,14 @@ export class OrganizationRepository extends DtoRepository<
typeof Organization,
[session: Session]
>(Organization) {
async create(input: CreateOrganization) {
async create(input: CreateOrganization, session: Session) {
if (!(await this.isUnique(input.name))) {
throw new DuplicateException(
'organization.name',
'Organization with this name already exists',
);
}

const initialProps = {
name: input.name,
acronym: input.acronym,
Expand All @@ -43,14 +55,18 @@ export class OrganizationRepository extends DtoRepository<
.apply(await createNode(Organization, { initialProps }))
.return<{ id: ID }>('node.id as id');

return await query.first();
const result = await query.first();
if (!result) {
throw new ServerException('Failed to create organization');
}

return await this.readOne(result.id, session);
}

async update(
existing: Organization,
changes: ChangesOf<Organization, UpdateOrganization>,
) {
return await this.updateProperties(existing, changes);
async update(changes: UpdateOrganization, session: Session) {
const { id, ...simpleChanges } = changes;
await this.updateProperties({ id }, simpleChanges);
return await this.readOne(id, session);
}

protected hydrate(session: Session) {
Expand Down
Loading
Loading