mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-25 00:07:47 +01:00
refactor: use promise.all instead of sequential awaited calls (#8316)
This PR follows up on a comment made in https://github.com/Unleash/unleash/pull/8314 and groups sequential awaited calls into a single Promise.all instead.
This commit is contained in:
parent
e51e6cc507
commit
4c4b4aa922
@ -12,6 +12,7 @@ import type { IPrivateProjectChecker } from '../private-project/privateProjectCh
|
|||||||
import type {
|
import type {
|
||||||
IAccessStore,
|
IAccessStore,
|
||||||
IAccountStore,
|
IAccountStore,
|
||||||
|
IEvent,
|
||||||
IEventStore,
|
IEventStore,
|
||||||
IOnboardingReadModel,
|
IOnboardingReadModel,
|
||||||
MinimalUser,
|
MinimalUser,
|
||||||
@ -19,6 +20,7 @@ import type {
|
|||||||
import type { FeatureEventFormatter } from '../../addons/feature-event-formatter-md';
|
import type { FeatureEventFormatter } from '../../addons/feature-event-formatter-md';
|
||||||
import { generateImageUrl } from '../../util';
|
import { generateImageUrl } from '../../util';
|
||||||
import type { PersonalDashboardProjectDetailsSchema } from '../../openapi';
|
import type { PersonalDashboardProjectDetailsSchema } from '../../openapi';
|
||||||
|
import type { IRoleWithProject } from '../../types/stores/access-store';
|
||||||
|
|
||||||
export class PersonalDashboardService {
|
export class PersonalDashboardService {
|
||||||
private personalDashboardReadModel: IPersonalDashboardReadModel;
|
private personalDashboardReadModel: IPersonalDashboardReadModel;
|
||||||
@ -103,44 +105,47 @@ export class PersonalDashboardService {
|
|||||||
userId: number,
|
userId: number,
|
||||||
projectId: string,
|
projectId: string,
|
||||||
): Promise<PersonalDashboardProjectDetailsSchema> {
|
): Promise<PersonalDashboardProjectDetailsSchema> {
|
||||||
const recentEvents = await this.eventStore.searchEvents(
|
const formatEvents = (recentEvents: IEvent[]) =>
|
||||||
{ limit: 4, offset: 0 },
|
recentEvents.map((event) => ({
|
||||||
[{ field: 'project', operator: 'IS', values: [projectId] }],
|
summary: this.featureEventFormatter.format(event).text,
|
||||||
);
|
createdBy: event.createdBy,
|
||||||
|
id: event.id,
|
||||||
const onboardingStatus =
|
createdByImageUrl: generateImageUrl({ email: event.createdBy }),
|
||||||
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 healthScores =
|
const filterRoles = (allRoles: IRoleWithProject[]) =>
|
||||||
await this.personalDashboardReadModel.getLatestHealthScores(
|
allRoles
|
||||||
projectId,
|
.filter((role) => ['project', 'custom'].includes(role.type))
|
||||||
8,
|
.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 avgHealthCurrentWindow: number | null = null;
|
||||||
let avgHealthPastWindow: number | null = null;
|
let avgHealthPastWindow: number | null = null;
|
||||||
|
|
||||||
@ -161,10 +166,10 @@ export class PersonalDashboardService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
latestEvents: formattedEvents,
|
latestEvents,
|
||||||
onboardingStatus,
|
onboardingStatus,
|
||||||
owners,
|
owners,
|
||||||
roles: projectRoles,
|
roles,
|
||||||
insights: {
|
insights: {
|
||||||
avgHealthCurrentWindow,
|
avgHealthCurrentWindow,
|
||||||
avgHealthPastWindow,
|
avgHealthPastWindow,
|
||||||
|
Loading…
Reference in New Issue
Block a user