[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,65 +457,70 @@ export const generateAnalysis = async (params: {
} }
}); });
const companyAnalysis = await Promise.all( Promise.all([
Array.from(companyMap.values()).map(async (companyOffer) => { generateAnalysisUnit(ctx.prisma, overallHighestOffer, usersOfferIds),
return await generateAnalysisUnit( Promise.all(
ctx.prisma, Array.from(companyMap.values()).map(async (companyOffer) => {
companyOffer, return await generateAnalysisUnit(
usersOfferIds, ctx.prisma,
true, companyOffer,
); usersOfferIds,
}), 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) => {
return { return {
analysedOffer: {
connect: {
id: analysisUnit.analysedOfferId,
},
},
noOfSimilarOffers: analysisUnit.noOfSimilarOffers,
percentile: analysisUnit.percentile,
topSimilarOffers: {
connect: analysisUnit.topSimilarOffers.map((offer) => {
return { id: offer.id };
}),
},
};
}),
},
overallAnalysis: {
create: {
analysedOffer: { analysedOffer: {
connect: { connect: {
id: analysisUnit.analysedOfferId, id: overallAnalysisUnit.analysedOfferId,
}, },
}, },
noOfSimilarOffers: analysisUnit.noOfSimilarOffers, noOfSimilarOffers: overallAnalysisUnit.noOfSimilarOffers,
percentile: analysisUnit.percentile, percentile: overallAnalysisUnit.percentile,
topSimilarOffers: { topSimilarOffers: {
connect: analysisUnit.topSimilarOffers.map((offer) => { connect: overallAnalysisUnit.topSimilarOffers.map((offer) => {
return { id: offer.id }; return { id: offer.id };
}), }),
}, },
};
}),
},
overallAnalysis: {
create: {
analysedOffer: {
connect: {
id: overallAnalysisUnit.analysedOfferId,
},
},
noOfSimilarOffers: overallAnalysisUnit.noOfSimilarOffers,
percentile: overallAnalysisUnit.percentile,
topSimilarOffers: {
connect: overallAnalysisUnit.topSimilarOffers.map((offer) => {
return { id: offer.id };
}),
}, },
}, },
}, overallHighestOffer: {
overallHighestOffer: { connect: {
connect: { id: overallHighestOffer.id,
id: overallHighestOffer.id, },
}, },
}, profile: {
profile: { connect: {
connect: { id: input.profileId,
id: input.profileId, },
}, },
}, },
}, include: analysisInclusion,
include: analysisInclusion, });
}); });
return profileAnalysisDtoMapper(analysis); return profileAnalysisDtoMapper(analysis);

Loading…
Cancel
Save