Skip to content
Merged
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
21 changes: 14 additions & 7 deletions server/scopeSummary/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ const persistScopeSummaryForId = async (
return newScopeSummary.id;
};

type ModelWithScopeSummary = Community | Collection | Pub;

const persistScopeSummaryForModel = async (
model: any,
model: ModelWithScopeSummary,
summary: CreationAttributes<ScopeSummary>,
) => {
model.scopeSummaryId = await persistScopeSummaryForId(model.scopeSummaryId, summary);
await model.save({ hooks: false });
};

export const summarizeCommunity = async (communityId: string) => {
const community = await Community.findOne({ where: { id: communityId } });
const community = await Community.findOne({ where: { id: communityId }, useMaster: true });

const pubs = await Pub.findAll({
where: { communityId },
Expand All @@ -48,20 +50,22 @@ export const summarizeCommunity = async (communityId: string) => {
where: { status: { [Op.ne]: 'incomplete' } },
},
],
useMaster: true,
});
const collections = await Collection.count({ where: { communityId } });
const collections = await Collection.count({ where: { communityId }, useMaster: true });
const submissions = pubs.filter((pub) => !!pub.submission);

const pubsInCommunity = await Pub.findAll({
where: { communityId },
include: 'scopeSummary',
useMaster: true,
});

const scopeSummaries = pubsInCommunity
.map((pub) => pub.scopeSummary)
.filter((x): x is ScopeSummary => !!x);

return persistScopeSummaryForModel(community, {
return persistScopeSummaryForModel(expect(community), {
...addScopeSummaries(...scopeSummaries),
pubs: pubs.length,
submissions: submissions.length,
Expand All @@ -74,6 +78,7 @@ export const summarizeCollection = async (collectionId: string) => {
await Collection.findOne({
where: { id: collectionId },
include: 'submissionWorkflow',
useMaster: true,
}),
) as types.DefinitelyHas<Collection, 'submissionWorkflow'>;

Expand All @@ -93,6 +98,7 @@ export const summarizeCollection = async (collectionId: string) => {
],
},
],
useMaster: true,
})) as (Omit<CollectionPub, 'pub'> & { pub: types.DefinitelyHas<Pub, 'scopeSummary'> })[];

const submissions = collectionPubs.filter(
Expand All @@ -113,10 +119,10 @@ export const summarizeCollection = async (collectionId: string) => {
};

export const summarizePub = async (pubId: string, summarizeParentScopes = true) => {
const pub = expect(await Pub.findOne({ where: { id: pubId } }));
const pub = expect(await Pub.findOne({ where: { id: pubId }, useMaster: true }));
const [discussions, reviews] = await Promise.all([
Discussion.count({ where: { pubId } }),
ReviewNew.count({ where: { pubId } }),
Discussion.count({ where: { pubId }, useMaster: true }),
ReviewNew.count({ where: { pubId }, useMaster: true }),
]);
await persistScopeSummaryForModel(pub, {
discussions,
Expand All @@ -128,6 +134,7 @@ export const summarizePub = async (pubId: string, summarizeParentScopes = true)
if (summarizeParentScopes) {
const collectionPubs = await CollectionPub.findAll({
where: { pubId },
useMaster: true,
});
await asyncMap(
collectionPubs,
Expand Down