Skip to content

Commit

Permalink
Made changes based on Bryans review
Browse files Browse the repository at this point in the history
  • Loading branch information
willdch committed Oct 10, 2023
1 parent 6849565 commit f0588e0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/components/location/dto/location.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Location extends Resource {
export class SecuredLocation extends SecuredProperty(Location) {}

@ObjectType({
description: SecuredPropertyList.descriptionFor('a location or null'),
description: SecuredPropertyList.descriptionFor('a list of locations'),
})
export class SecuredLocations extends SecuredPropertyList(Location) {}

Expand Down
4 changes: 2 additions & 2 deletions src/components/partner/dto/create-partner.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ export abstract class CreatePartner {
@Field(() => [IDType], { nullable: true })
@IsId({ each: true })
@Transform(({ value }) => uniq(value))
readonly fieldRegions?: ReadonlyArray<IdOf<FieldRegion>> = [];
readonly countries?: ReadonlyArray<IdOf<Location>> = [];

@Field(() => [IDType], { nullable: true })
@IsId({ each: true })
@Transform(({ value }) => uniq(value))
readonly countries?: ReadonlyArray<IdOf<Location>> = [];
readonly fieldRegions?: ReadonlyArray<IdOf<FieldRegion>> = [];
}

@InputType()
Expand Down
11 changes: 6 additions & 5 deletions src/components/partner/dto/update-partner.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Transform, Type } from 'class-transformer';
import { Matches, ValidateNested } from 'class-validator';
import { uniq } from 'lodash';
import { ID, IdField, IdOf, IsId, NameField } from '../../../common';
import { FieldRegion } from '../../field-region';
import { Location } from '../../../components/location';
import { FieldRegion } from '../../field-region';
import type { Language } from '../../language';
import { FinancialReportingType } from '../../partnership/dto/financial-reporting-type';
import { PartnerType } from './partner-type.enum';
Expand All @@ -13,7 +13,7 @@ import { Partner } from './partner.dto';
@InputType()
export abstract class UpdatePartner {
@IdField()
readonly id: CordID;
readonly id: ID;

@IdField({ nullable: true })
readonly pointOfContactId?: ID | null;
Expand Down Expand Up @@ -47,11 +47,12 @@ export abstract class UpdatePartner {
@Field(() => [IDType], { nullable: true })
@IsId({ each: true })
@Transform(({ value }) => (value ? uniq(value) : undefined))
readonly fieldRegions?: ReadonlyArray<IdOf<FieldRegion>>;
readonly countries?: ReadonlyArray<IdOf<Location>>;

@Field(() => [ID], { nullable: true })
@Field(() => [IDType], { nullable: true })
@IsId({ each: true })
@Transform(({ value }) => (value ? uniq(value) : undefined))
readonly countries?: ReadonlyArray<IdOf<Location>>;
readonly fieldRegions?: ReadonlyArray<IdOf<FieldRegion>>;
}

@InputType()
Expand Down
18 changes: 2 additions & 16 deletions src/components/partner/partner.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
loadSecuredIds,
LoggedInSession,
mapSecuredValue,
NotFoundException,
Session,
} from '../../common';
import { Loader, LoaderOf } from '../../core';
Expand Down Expand Up @@ -104,26 +103,13 @@ export class PartnerResolver {
): Promise<SecuredFieldRegions> {
return await loadSecuredIds(loader, partner.fieldRegions);
}

@ResolveField(() => SecuredLocations)
async countries(
@Parent() partner: Partner,
@Loader(LocationLoader) loader: LoaderOf<LocationLoader>,
): Promise<SecuredLocations> {
const countries = (await loader.loadMany(partner.countries.value)).flatMap(
(country) => {
if (country instanceof NotFoundException) {
return [];
} else if (country instanceof Error) {
throw country;
}

return country;
},
);
return {
...partner.countries,
value: countries,
};
return await loadSecuredIds(loader, partner.countries);
}

@ResolveField(() => SecuredProjectList, {
Expand Down
59 changes: 14 additions & 45 deletions src/components/partner/partner.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {
DuplicateException,
ID,
InputException,
many,
Many,
NotFoundException,
ObjectView,
ServerException,
Expand All @@ -15,11 +13,6 @@ import {
import { HandleIdLookup, ILogger, Logger } from '../../core';
import { mapListResults } from '../../core/database/results';
import { Privileges } from '../authorization';
import {
LocationListInput,
LocationService,
SecuredLocationList,
} from '../location';
import { FinancialReportingType } from '../partnership/dto/financial-reporting-type';
import {
IProject,
Expand All @@ -40,7 +33,6 @@ import { PartnerRepository } from './partner.repository';
@Injectable()
export class PartnerService {
constructor(
private readonly countriesService: LocationService,
@Logger('partner:service') private readonly logger: ILogger,
private readonly privileges: Privileges,
@Inject(forwardRef(() => ProjectService))
Expand Down Expand Up @@ -157,6 +149,20 @@ export class PartnerService {
);
}

if (countries) {
try {
await this.repo.updateRelationList({
id: partner.id,
relation: 'countries',
newList: countries,
});
} catch (e) {
throw e instanceof InputException
? e.withField('partner.countries')
: e;
}
}

if (fieldRegions) {
try {
await this.repo.updateRelationList({
Expand All @@ -174,26 +180,6 @@ export class PartnerService {
return await this.readOne(input.id, session);
}

protected async validateCountryIds(
ids: Many<ID> | null | undefined,
label: string,
resourceField: string,
errMsg: string,
): Promise<void> {
await Promise.all(
many(ids ?? []).map(async (id, index) => {
const country = await this.repo.getBaseNode(id, label);
if (country) {
return;
}
throw new NotFoundException(
errMsg,
`partner.${resourceField}${Array.isArray(ids) ? `[${index}]` : ''}`,
);
}),
);
}

async delete(id: ID, session: Session): Promise<void> {
const object = await this.readOne(id, session);
if (!object) {
Expand Down Expand Up @@ -242,23 +228,6 @@ export class PartnerService {
};
}

async listCountries(
partner: Partner,
input: LocationListInput,
session: Session,
): Promise<SecuredLocationList> {
const countriesListOutput = await this.countriesService.list(
{ ...input, filter: { ...input.filter } },
session,
);

return {
...countriesListOutput,
canRead: true,
canCreate: this.privileges.for(session, IProject).can('create'),
};
}

protected verifyFinancialReportingType(
financialReportingTypes: FinancialReportingType[] | undefined,
types: PartnerType[] | undefined,
Expand Down

0 comments on commit f0588e0

Please sign in to comment.