perf: batch gateway submits and isolate callbacks
This commit is contained in:
@@ -22,18 +22,25 @@ export class PhoneRoutingLookupService {
|
||||
}
|
||||
|
||||
async identifyProvince(phoneNumber: string) {
|
||||
const prefixes = phonePrefixes(phoneNumber);
|
||||
if (prefixes.length === 0) return null;
|
||||
return (await this.identifyProvinces([phoneNumber])).get(phoneNumber) ?? null;
|
||||
}
|
||||
|
||||
async identifyProvinces(phoneNumbers: string[]) {
|
||||
const uniquePhones = [...new Set(phoneNumbers)];
|
||||
const prefixesByPhone = new Map(uniquePhones.map((phone) => [phone, phonePrefixes(phone)]));
|
||||
const prefixes = [...new Set([...prefixesByPhone.values()].flat())];
|
||||
if (prefixes.length === 0) return new Map(uniquePhones.map((phone) => [phone, null]));
|
||||
const segments = await this.prisma.phoneSegment.findMany({
|
||||
where: { prefix: { in: prefixes } },
|
||||
select: { prefix: true, province: true },
|
||||
});
|
||||
const provinceByPrefix = new Map(segments.map((segment) => [segment.prefix, segment.province]));
|
||||
for (const prefix of prefixes) {
|
||||
const province = provinceByPrefix.get(prefix);
|
||||
if (province) return province;
|
||||
}
|
||||
return null;
|
||||
return new Map(uniquePhones.map((phone) => {
|
||||
const province = (prefixesByPhone.get(phone) ?? [])
|
||||
.map((prefix) => provinceByPrefix.get(prefix))
|
||||
.find((value): value is string => Boolean(value)) ?? null;
|
||||
return [phone, province];
|
||||
}));
|
||||
}
|
||||
|
||||
invalidateCarrierRules() {
|
||||
|
||||
Reference in New Issue
Block a user