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

refactor: use promise.all instead of sequential awaited calls #8316

Merged
merged 4 commits into from
Oct 2, 2024
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
81 changes: 43 additions & 38 deletions src/lib/features/personal-dashboard/personal-dashboard-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import type { IPrivateProjectChecker } from '../private-project/privateProjectCh
import type {
IAccessStore,
IAccountStore,
IEvent,
IEventStore,
IOnboardingReadModel,
MinimalUser,
} from '../../types';
import type { FeatureEventFormatter } from '../../addons/feature-event-formatter-md';
import { generateImageUrl } from '../../util';
import type { PersonalDashboardProjectDetailsSchema } from '../../openapi';
import type { IRoleWithProject } from '../../types/stores/access-store';

export class PersonalDashboardService {
private personalDashboardReadModel: IPersonalDashboardReadModel;
Expand Down Expand Up @@ -103,44 +105,47 @@ export class PersonalDashboardService {
userId: number,
projectId: string,
): Promise<PersonalDashboardProjectDetailsSchema> {
const recentEvents = await this.eventStore.searchEvents(
{ limit: 4, offset: 0 },
[{ field: 'project', operator: 'IS', values: [projectId] }],
);

const onboardingStatus =
await this.onboardingReadModel.getOnboardingStatusForProject(
projectId,
);

const formattedEvents = recentEvents.map((event) => ({
summary: this.featureEventFormatter.format(event).text,
createdBy: event.createdBy,
id: event.id,
createdByImageUrl: generateImageUrl({ email: event.createdBy }),
}));

const owners =
await this.projectOwnersReadModel.getProjectOwners(projectId);

const allRoles = await this.accessStore.getAllProjectRolesForUser(
userId,
projectId,
);

const projectRoles = allRoles
.filter((role) => ['project', 'custom'].includes(role.type))
.map((role) => ({
id: role.id,
name: role.name,
type: role.type as PersonalDashboardProjectDetailsSchema['roles'][number]['type'],
const formatEvents = (recentEvents: IEvent[]) =>
recentEvents.map((event) => ({
summary: this.featureEventFormatter.format(event).text,
createdBy: event.createdBy,
id: event.id,
createdByImageUrl: generateImageUrl({ email: event.createdBy }),
}));

const healthScores =
await this.personalDashboardReadModel.getLatestHealthScores(
projectId,
8,
);
const filterRoles = (allRoles: IRoleWithProject[]) =>
allRoles
.filter((role) => ['project', 'custom'].includes(role.type))
.map((role) => ({
id: role.id,
name: role.name,
type: role.type as PersonalDashboardProjectDetailsSchema['roles'][number]['type'],
}));

const [latestEvents, onboardingStatus, owners, roles, healthScores] =
await Promise.all([
this.eventStore
.searchEvents({ limit: 4, offset: 0 }, [
{
field: 'project',
operator: 'IS',
values: [projectId],
},
])
.then(formatEvents),
this.onboardingReadModel.getOnboardingStatusForProject(
projectId,
),
this.projectOwnersReadModel.getProjectOwners(projectId),
this.accessStore
.getAllProjectRolesForUser(userId, projectId)
.then(filterRoles),
this.personalDashboardReadModel.getLatestHealthScores(
projectId,
8,
),
]);

let avgHealthCurrentWindow: number | null = null;
let avgHealthPastWindow: number | null = null;

Expand All @@ -161,10 +166,10 @@ export class PersonalDashboardService {
}

return {
latestEvents: formattedEvents,
latestEvents,
onboardingStatus,
owners,
roles: projectRoles,
roles,
insights: {
avgHealthCurrentWindow,
avgHealthPastWindow,
Expand Down