[offers][refactor] Calculate overall and company analyses asychronously ()

* [offers][refactor] Calculate overall and company analyses asychronously

* [offers][refactor] Calculate overall and company analyses asychronously
ailing/undo-fix
Bryann Yeap Kok Keong 2 years ago committed by GitHub
parent 123a4cc840
commit 31a166e6e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -330,7 +330,7 @@ export const profileAnalysisDtoMapper = (
}) })
| null, | null,
) => { ) => {
if (!analysis) { if (analysis == null) {
return null; return null;
} }

@ -3,6 +3,8 @@ import type {
City, City,
Company, Company,
Country, Country,
OffersAnalysis,
OffersAnalysisUnit,
OffersBackground, OffersBackground,
OffersCurrency, OffersCurrency,
OffersExperience, OffersExperience,
@ -20,6 +22,98 @@ import { TRPCError } from '@trpc/server';
import { analysisInclusion } from './analysisInclusion'; import { analysisInclusion } from './analysisInclusion';
import { profileAnalysisDtoMapper } from '../../../mappers/offers-mappers'; import { profileAnalysisDtoMapper } from '../../../mappers/offers-mappers';
type Analysis =
| (OffersAnalysis & {
companyAnalysis: Array<
OffersAnalysisUnit & {
analysedOffer: OffersOffer & {
company: Company;
offersFullTime:
| (OffersFullTime & { totalCompensation: OffersCurrency })
| null;
offersIntern:
| (OffersIntern & { monthlySalary: OffersCurrency })
| null;
profile: OffersProfile & { background: OffersBackground | null };
};
topSimilarOffers: Array<
OffersOffer & {
company: Company;
location: City & { state: State & { country: Country } };
offersFullTime:
| (OffersFullTime & { totalCompensation: OffersCurrency })
| null;
offersIntern:
| (OffersIntern & { monthlySalary: OffersCurrency })
| null;
profile: OffersProfile & {
background:
| (OffersBackground & {
experiences: Array<
OffersExperience & {
company: Company | null;
location:
| (City & { state: State & { country: Country } })
| null;
}
>;
})
| null;
};
}
>;
}
>;
overallAnalysis: OffersAnalysisUnit & {
analysedOffer: OffersOffer & {
company: Company;
offersFullTime:
| (OffersFullTime & { totalCompensation: OffersCurrency })
| null;
offersIntern:
| (OffersIntern & { monthlySalary: OffersCurrency })
| null;
profile: OffersProfile & { background: OffersBackground | null };
};
topSimilarOffers: Array<
OffersOffer & {
company: Company;
location: City & { state: State & { country: Country } };
offersFullTime:
| (OffersFullTime & { totalCompensation: OffersCurrency })
| null;
offersIntern:
| (OffersIntern & { monthlySalary: OffersCurrency })
| null;
profile: OffersProfile & {
background:
| (OffersBackground & {
experiences: Array<
OffersExperience & {
company: Company | null;
location:
| (City & { state: State & { country: Country } })
| null;
}
>;
})
| null;
};
}
>;
};
overallHighestOffer: OffersOffer & {
company: Company;
location: City & { state: State & { country: Country } };
offersFullTime:
| (OffersFullTime & { totalCompensation: OffersCurrency })
| null;
offersIntern: (OffersIntern & { monthlySalary: OffersCurrency }) | null;
profile: OffersProfile & { background: OffersBackground | null };
};
})
| null;
type Offer = OffersOffer & { type Offer = OffersOffer & {
company: Company; company: Company;
location: City & { state: State & { country: Country } }; location: City & { state: State & { country: Country } };
@ -285,6 +379,8 @@ export const generateAnalysis = async (params: {
}; };
input: { profileId: string }; input: { profileId: string };
}) => { }) => {
let analysis: Analysis = null;
const { ctx, input } = params; const { ctx, input } = params;
await ctx.prisma.offersAnalysis.deleteMany({ await ctx.prisma.offersAnalysis.deleteMany({
where: { where: {
@ -352,17 +448,8 @@ export const generateAnalysis = async (params: {
} }
const overallHighestOffer = offers[0]; const overallHighestOffer = offers[0];
const usersOfferIds = offers.map((offer) => offer.id); const usersOfferIds = offers.map((offer) => offer.id);
// OVERALL ANALYSIS
const overallAnalysisUnit = await generateAnalysisUnit(
ctx.prisma,
overallHighestOffer,
usersOfferIds,
);
// COMPANY ANALYSIS
const companyMap = new Map<string, Offer>(); const companyMap = new Map<string, Offer>();
offers.forEach((offer) => { offers.forEach((offer) => {
if (companyMap.get(offer.companyId) == null) { if (companyMap.get(offer.companyId) == null) {
@ -370,7 +457,9 @@ export const generateAnalysis = async (params: {
} }
}); });
const companyAnalysis = await Promise.all( Promise.all([
generateAnalysisUnit(ctx.prisma, overallHighestOffer, usersOfferIds),
Promise.all(
Array.from(companyMap.values()).map(async (companyOffer) => { Array.from(companyMap.values()).map(async (companyOffer) => {
return await generateAnalysisUnit( return await generateAnalysisUnit(
ctx.prisma, ctx.prisma,
@ -379,9 +468,11 @@ export const generateAnalysis = async (params: {
true, true,
); );
}), }),
); ),
]).then(async (analyses) => {
const [overallAnalysisUnit, companyAnalysis] = analyses;
const analysis = await ctx.prisma.offersAnalysis.create({ analysis = await ctx.prisma.offersAnalysis.create({
data: { data: {
companyAnalysis: { companyAnalysis: {
create: companyAnalysis.map((analysisUnit) => { create: companyAnalysis.map((analysisUnit) => {
@ -430,6 +521,7 @@ export const generateAnalysis = async (params: {
}, },
include: analysisInclusion, include: analysisInclusion,
}); });
});
return profileAnalysisDtoMapper(analysis); return profileAnalysisDtoMapper(analysis);
}; };

Loading…
Cancel
Save