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

849 refactor product service #875

Merged
merged 7 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 1 addition & 4 deletions src/components/budget/budget.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { ISession, Order } from '../../common';
import {
addAllSecureProperties,
addBaseNodeMetaPropsWithClause,
addUserToSG,
ConfigService,
createBaseNode,
DatabaseService,
Expand Down Expand Up @@ -194,16 +193,14 @@ export class BudgetService {
.query()
.call(matchRequestingUser, session)
.match([
node('rootUser', 'User', {
node('root', 'User', {
active: true,
id: this.config.rootAdmin.id,
}),
])
.call(createBaseNode, 'Budget', secureProps, {
owningOrgId: session.owningOrgId,
})
.call(addUserToSG, 'rootUser', 'adminSG')
.call(addUserToSG, 'rootUser', 'readerSG')
.return('node.id as id');

const result = await createBudget.first();
Expand Down
5 changes: 1 addition & 4 deletions src/components/ceremony/ceremony.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { ISession } from '../../common';
import {
addAllSecureProperties,
addBaseNodeMetaPropsWithClause,
addUserToSG,
ConfigService,
createBaseNode,
DatabaseService,
Expand Down Expand Up @@ -172,16 +171,14 @@ export class CeremonyService {
.query()
.call(matchRequestingUser, session)
.match([
node('rootUser', 'User', {
node('root', 'User', {
active: true,
id: this.config.rootAdmin.id,
}),
])
.call(createBaseNode, 'Ceremony', secureProps, {
owningOrgId: session.owningOrgId,
})
.call(addUserToSG, 'rootUser', 'adminSG')
.call(addUserToSG, 'rootUser', 'readerSG')
.return('node.id as id');

const result = await query.first();
Expand Down
118 changes: 105 additions & 13 deletions src/components/film/film.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import {
NotFoundException,
InternalServerErrorException as ServerException,
} from '@nestjs/common';
import { node, relation } from 'cypher-query-builder';
import { inArray, node, relation } from 'cypher-query-builder';
import { DateTime } from 'luxon';
import { DuplicateException, ISession } from '../../common';
import {
addAllSecureProperties,
addBaseNodeMetaPropsWithClause,
addUserToSG,
ConfigService,
createBaseNode,
DatabaseService,
Expand All @@ -23,6 +22,11 @@ import {
OnIndex,
runListQuery,
} from '../../core';
import { ScriptureRange } from '../scripture';
import {
scriptureToVerseRange,
verseToScriptureRange,
} from '../scripture/reference';
import {
CreateFilm,
Film,
Expand Down Expand Up @@ -163,18 +167,32 @@ export class FilmService {
.query()
.call(matchRequestingUser, session)
.match([
node('rootuser', 'User', {
node('root', 'User', {
active: true,
id: this.config.rootAdmin.id,
}),
])
.call(createBaseNode, ['Film', 'Producible'], secureProps, {
owningOrgId: session.owningOrgId,
})
.create([...this.permission('range', 'node')])
.call(addUserToSG, 'rootuser', 'adminSG')
.call(addUserToSG, 'rootuser', 'readerSG')
.return('node.id as id');
.create([...this.permission('range', 'node')]);

if (input.scriptureReferences) {
for (const sr of input.scriptureReferences) {
const verseRange = scriptureToVerseRange(sr);
query.create([
node('node'),
relation('out', '', 'scriptureReferences', { active: true }),
node('sr', 'ScriptureRange', {
start: verseRange.start,
end: verseRange.end,
active: true,
createdAt: DateTime.local().toString(),
}),
]);
}
}
query.return('node.id as id');

const result = await query.first();
if (!result) {
Expand All @@ -190,19 +208,43 @@ export class FilmService {
}

async readOne(filmId: string, session: ISession): Promise<Film> {
const secureProps = ['name', 'range'];
const secureProps = ['name'];
const baseNodeMetaProps = ['id', 'createdAt'];
const readFilm = this.db
.query()
.call(matchRequestingUser, session)
.call(matchUserPermissions, 'Film', filmId)
.call(addAllSecureProperties, ...secureProps)
.optionalMatch([
node('scriptureReferencesReadPerm', 'Permission', {
property: 'scriptureReferences',
read: true,
active: true,
}),
relation('out', '', 'baseNode'),
node('node'),
relation('out', '', 'scriptureReferences', { active: true }),
node('scriptureReferences', 'ScriptureRange', { active: true }),
])
.where({ scriptureReferencesReadPerm: inArray(['permList'], true) })
.optionalMatch([
node('scriptureReferencesEditPerm', 'Permission', {
property: 'scriptureReferences',
edit: true,
active: true,
}),
relation('out', '', 'baseNode'),
node('node'),
])
.where({ scriptureReferencesReadPerm: inArray(['permList'], true) })
.return(
`
{
${addBaseNodeMetaPropsWithClause(baseNodeMetaProps)},
${listWithSecureObject(secureProps)},
canReadFilms: requestingUser.canReadFilms
canReadFilms: requestingUser.canReadFilms,
canScriptureReferencesRead: scriptureReferencesReadPerm.read,
canScriptureReferencesEdit: scriptureReferencesEditPerm.edit
} as film
`
);
Expand All @@ -218,14 +260,18 @@ export class FilmService {
);
}

const scriptureReferences = await this.listScriptureReferences(
result.film.id,
session
);

return {
id: result.film.id,
name: result.film.name,
scriptureReferences: {
// TODO
canRead: !!result.film.range.canRead,
canEdit: !!result.film.range.canEdit,
value: [],
canRead: !!result.film.canScriptureReferencesRead,
canEdit: !!result.film.canScriptureReferencesEdit,
value: scriptureReferences,
},
createdAt: result.film.createdAt,
};
Expand Down Expand Up @@ -294,4 +340,50 @@ export class FilmService {
total: listResult.total,
};
}

async listScriptureReferences(
filmId: string,
session: ISession
): Promise<ScriptureRange[]> {
const query = this.db
.query()
.match([
node('film', 'Film', {
id: filmId,
active: true,
owningOrgId: session.owningOrgId,
}),
relation('out', '', 'scriptureReferences'),
node('scriptureRanges', 'ScriptureRange', { active: true }),
])
.with('collect(scriptureRanges) as items')
.return('items');
const result = await query.first();

if (!result) {
return [];
}

const items: ScriptureRange[] = await Promise.all(
result.items.map(
(item: {
identity: string;
labels: string;
properties: {
start: number;
end: number;
createdAt: string;
active: boolean;
};
}) => {
return verseToScriptureRange({
start: item.properties.start,
end: item.properties.end,
});
}
)
);

return items;
}
}
4 changes: 4 additions & 0 deletions src/components/product/dto/list-product.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export abstract class ProductFilters {
})
readonly methodology?: ProductMethodology;

@Field({
description: 'Only products for this engagement',
nullable: true,
})
tjgersho marked this conversation as resolved.
Show resolved Hide resolved
readonly engagementId?: string; // TODO
}

Expand Down
Loading