Skip to content

Commit

Permalink
fix: missing schema fetching issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jroehl committed Nov 2, 2022
1 parent 8e95a93 commit 77fc39a
Show file tree
Hide file tree
Showing 8 changed files with 2,519 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v12.13.0
v14.18.1
22 changes: 11 additions & 11 deletions src/classes/portals/FlowFact/v2/Aggregator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { flatten, isObject, cloneDeep } from 'lodash';
import { cloneDeep, flatten, isObject } from 'lodash';

import { Mapping, Estate } from '../../Estate';
import estateCommon from '../../../../translations';
import { AvailableLanguages } from '../../../../types';
import { TokenAuth } from '../../../Authorization';
import { Portal, FetchOptions } from '../../Portal';
import { FlowFactEstateV2 } from './Estate';
import { Aggregator } from '../../Aggregator';
import { AvailableLanguages } from '../../../../types';
import FlowFactPortalV2, { estateSchemas } from './Portal';
import { Estate, Mapping } from '../../Estate';
import { FetchOptions, Portal } from '../../Portal';
import { FlowFactEstateV2 } from './Estate';
import FlowFactPortalV2 from './Portal';
import { enrichResultWithReadableKeys } from './utils';
import estateCommon from '../../../../translations';

const safeEstateCommon = cloneDeep(estateCommon);

Expand Down Expand Up @@ -68,15 +68,15 @@ export class FlowFactV2 extends Aggregator {
): Promise<Mapping> {
const dictionary = this.dictionaries[language];
if (isObject(dictionary)) return dictionary;
const schemas = await (this.portal as FlowFactPortalV2).fetchSchemas();
const reducedSchemas = schemas.filter(({ name }) =>
estateSchemas.includes(name)
const schemas = await (this.portal as FlowFactPortalV2).fetchSchemas(
undefined,
['estates']
);
const parseFields = initFieldParse(language, {
...safeEstateCommon.fallbacks.en,
...safeEstateCommon[language || 'en'],
});
const fields = flatten(parseFields(reducedSchemas));
const fields = flatten(parseFields(schemas));
const result = Object.assign({}, ...fields);
this.dictionaries[language] = result;
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/classes/portals/FlowFact/v2/Estate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class FlowFactEstateV2 extends Estate {
externalID: this.getValue('identifier.values[0]'),
internalID: this.getValue('_metadata.id'),
livingSpace: this.getValue('livingarea.values[0]'),
totalArea: this.getValue('totalarea.values[0]'),
totalArea: this.getTranslatableValue('totalarea.values[0]'),
numberOfRooms: this.getValue('rooms.values[0]'),
price: this.getPrice(),
title: this.getTranslatableValue('headline.values[0]'),
Expand Down
74 changes: 33 additions & 41 deletions src/classes/portals/FlowFact/v2/Portal.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,14 @@
import { flatten } from 'lodash';
import requestPromise from 'request-promise-native';
import { Logger } from '../../../../utils';

import { Portal, FetchOptions } from '../../Portal';
import {
Authorization,
AuthorizationHeader,
Credentials,
TokenAuth,
TokenAuth
} from '../../../Authorization';

export const estateSchemas = [
'catering_accommodation_purchase',
'catering_accommodation_rent',
'flat_purchase',
'flat_rent',
'garage_pitch',
'garage_pitch_purchase',
'garage_pitch_rent',
'house_purchase',
'houses_rent',
'investment',
'land_lease',
'land_purchase',
'office_surgery_purchase',
'office_surgery_rent',
'other_commercial_estates_purchase',
'other_commercial_estates_rent',
'production_halls_purchase',
'production_halls_rent',
'shops_commerce_purchase',
'shops_commerce_rent',
'temporary_accommodation',
];
import { FetchOptions, Portal } from '../../Portal';

class FlowFactV2Authorization extends Authorization {
protected authorizationHeader?: AuthorizationHeader;
Expand All @@ -53,7 +30,7 @@ class FlowFactV2Authorization extends Authorization {
}

checkCredentials(credentials: Credentials): Credentials {
const basicAuth = (credentials as unknown) as TokenAuth;
const basicAuth = credentials as unknown as TokenAuth;
const isValid = Boolean(basicAuth.token);

if (!isValid) {
Expand Down Expand Up @@ -106,23 +83,31 @@ export default class FlowFactPortalV2 extends Portal {
}

async fetchEstates(options?: FetchOptions): Promise<any[]> {
const schemas = await this.fetchSchemas(undefined, ['estates']);

const items = await Promise.all(
estateSchemas.map(async (schemaID) => {
const results = await this.fetchRecursive(
`${this.baseURL}/search-service/stable/schemas/${schemaID}`,
{
method: 'POST',
body: {
target: 'ENTITY',
distinct: true,
schemas.map(async ({ id: schemaID }) => {
try {
return await this.fetchRecursive(
`${this.baseURL}/search-service/stable/schemas/${schemaID}`,
{
method: 'POST',
body: {
target: 'ENTITY',
distinct: true,
},
},
},
options
);
return results;
options
);
} catch (error) {
Logger.error((error as Error).message || error);
return [];
}
})
);

const flattened = flatten(items);

if (!options?.detailed) return flattened;
return Promise.all(
flattened.map(async (res: any) => {
Expand All @@ -138,8 +123,15 @@ export default class FlowFactPortalV2 extends Portal {
return this.request(uri, undefined, { id });
}

async fetchSchemas(options?: FetchOptions): Promise<any[]> {
async fetchSchemas(
options?: FetchOptions,
groups?: string[]
): Promise<any[]> {
const uri = `${this.baseURL}/schema-service/stable/v2/schemas/`;
return this.fetchRecursive(uri, undefined, options);
const res = await this.fetchRecursive(uri, undefined, options);
if (!groups?.length) return res;
return res.filter((schema: any) =>
schema.groups?.some((group: string) => groups.includes(group))
);
}
}
20 changes: 9 additions & 11 deletions src/classes/portals/FlowFact/v2/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ export default (uri: string, options: any) => {
if (uri.includes('authenticate')) return 'cognitoToken';
if (uri.includes('error')) throw new Error('Request error');

if (uri.includes('schemas/?')) {
return schemas;
}
if (uri.includes('schema-service')) return schemas;

if (uri.includes('schemas')) {
if (options.method === 'POST' && uri.includes('flat_rent')) {
if (
options.method === 'POST' &&
uri.includes('9f0ea8f8-228e-4ec5-9637-07c1760e2f51')
) {
if (uri.includes('size=2')) return estatesRecursive;
return estates;
}
Expand Down Expand Up @@ -94,8 +95,7 @@ export const getResultCommon = (translated: boolean = false): any => {
totalRent: undefined,
previewImage: {
title: 'Wohnzimmer',
url:
'https://s3.eu-central-1.amazonaws.com/cloudios.production.image/8b/91811a12-a594-42d4-bf75-bddb39c94b8b/6a/75a1bcea-49de-3d8d-97bb-05b600e1bb6a/17/a58a7f44-79dc-4177-8ed2-4bf589673117.JPG',
url: 'https://s3.eu-central-1.amazonaws.com/cloudios.production.image/8b/91811a12-a594-42d4-bf75-bddb39c94b8b/6a/75a1bcea-49de-3d8d-97bb-05b600e1bb6a/17/a58a7f44-79dc-4177-8ed2-4bf589673117.JPG',
},
updatedAt: 1580294614972,
};
Expand All @@ -110,13 +110,11 @@ export const getResultProperties = (translated: boolean = false): any => {
attachments: [
{
title: 'Wohnzimmer',
url:
'https://s3.eu-central-1.amazonaws.com/cloudios.production.image/8b/91811a12-a594-42d4-bf75-bddb39c94b8b/6a/75a1bcea-49de-3d8d-97bb-05b600e1bb6a/17/a58a7f44-79dc-4177-8ed2-4bf589673117.JPG',
url: 'https://s3.eu-central-1.amazonaws.com/cloudios.production.image/8b/91811a12-a594-42d4-bf75-bddb39c94b8b/6a/75a1bcea-49de-3d8d-97bb-05b600e1bb6a/17/a58a7f44-79dc-4177-8ed2-4bf589673117.JPG',
},
{
title: 'Carport',
url:
'https://s3.eu-central-1.amazonaws.com/cloudios.production.image/8b/91811a12-a594-42d4-bf75-bddb39c94b8b/6a/75a1bcea-49de-3d8d-97bb-05b600e1bb6a/91/65cacc57-abdd-4ff9-9b33-5e9391111491.JPG',
url: 'https://s3.eu-central-1.amazonaws.com/cloudios.production.image/8b/91811a12-a594-42d4-bf75-bddb39c94b8b/6a/75a1bcea-49de-3d8d-97bb-05b600e1bb6a/91/65cacc57-abdd-4ff9-9b33-5e9391111491.JPG',
},
],
attic: undefined,
Expand Down Expand Up @@ -161,7 +159,7 @@ export const getResultProperties = (translated: boolean = false): any => {
residentialUnits: undefined,
summerResidencePractical: undefined,
usableFloorSpace: undefined,
marketingType: "PURCHASE",
marketingType: 'PURCHASE',
};
if (translated) return translate(properties);
return properties;
Expand Down
Loading

0 comments on commit 77fc39a

Please sign in to comment.