trade360-nodejs-sdk
Version:
LSports Trade360 SDK for Node.js
150 lines • 12.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mapper = void 0;
const lodash_1 = require("lodash");
const _entities_1 = require("../../entities/core-entities/index.js");
const dtos_1 = require("./metadata/dtos");
const requests_1 = require("./metadata/requests");
const dtos_2 = require("./subscription/dtos");
const requests_2 = require("./subscription/requests");
const _utilities_1 = require("../../utilities");
const snapshot_1 = require("./snapshot");
/**
* Mapper class for mapping between different types
* of objects in the application using the mapping
* configurations provided in the application.
* The Mapper class provides functionality similar
* to AutoMapper with the ability to map between
* different types of objects in the application
* using the mapping configurations provided in the
* application.
* @param packageCredentials The package credentials
* for the API to use in the mapping configurations
* for the application. The package credentials are
* used to authenticate the API requests in the
* mapping configurations for the application.
* @implements IMapper interface for mapping between
* different types of objects in the application
* using the mapping configurations provided in the
* application.
*/
class Mapper {
constructor(packageCredentials) {
this.mappingConfigs = new Map();
this.desiredDateFormat = 'MM/DD/YYYY';
this.initializeMappings(packageCredentials);
}
map(source, destinationType) {
const key = `${source.constructor.name}-${destinationType.name}`;
const mappingFn = this.mappingConfigs.get(key);
if (mappingFn) {
return mappingFn(source);
}
return _utilities_1.TransformerUtil.transform(source, destinationType);
}
/**
* Initializes all mapping configurations for the
* application using the package credentials provided in
* the constructor of the mapping service to authenticate
* the API requests in the mapping configurations for the
* application.
* @param packageCredentials The package credentials for
* the API to use in the mapping configurations for the
* application.
* @returns void to indicate that the mapping
* configurations have been initialized successfully in
* the application using the package credentials provided
* in the constructor of the mapping service.
*/
initializeMappings(packageCredentials) {
this.registerMapping(snapshot_1.GetEventRequestDto, snapshot_1.GetEventRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, snapshot_1.GetEventRequest));
this.registerMapping(snapshot_1.GetFixtureRequestDto, snapshot_1.GetFixtureRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, snapshot_1.GetFixtureRequest));
this.registerMapping(snapshot_1.GetInPlayEventRequestDto, snapshot_1.GetInPlayEventRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, snapshot_1.GetInPlayEventRequest));
this.registerMapping(snapshot_1.GetLivescoreRequestDto, snapshot_1.GetLivescoreRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, snapshot_1.GetLivescoreRequest));
this.registerMapping(snapshot_1.GetMarketRequestDto, snapshot_1.GetMarketRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, snapshot_1.GetMarketRequest));
this.registerMapping(snapshot_1.GetOutrightEventRequestDto, snapshot_1.GetOutrightEventRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, snapshot_1.GetOutrightEventRequest));
this.registerMapping(snapshot_1.GetOutrightEventRequestDto, snapshot_1.GetOutrightEventRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, snapshot_1.GetOutrightEventRequest));
this.registerMapping(snapshot_1.GetOutrightFixtureRequestDto, snapshot_1.GetOutrightFixtureRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, snapshot_1.GetOutrightFixtureRequest));
this.registerMapping(snapshot_1.GetOutrightLeagueMarketRequestDto, snapshot_1.GetOutrightLeagueMarketRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, snapshot_1.GetOutrightLeagueMarketRequest));
this.registerMapping(snapshot_1.GetOutrightLeaguesRequestDto, snapshot_1.GetOutrightLeaguesRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, snapshot_1.GetOutrightLeaguesRequest));
this.registerMapping(snapshot_1.GetOutrightLivescoreRequestDto, snapshot_1.GetOutrightLivescoreRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, snapshot_1.GetOutrightLivescoreRequest));
this.registerMapping(snapshot_1.GetOutrightMarketRequestDto, snapshot_1.GetOutrightMarketRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, snapshot_1.GetOutrightMarketRequest));
this.registerMapping(dtos_1.GetLeaguesRequestDto, requests_1.GetLeaguesRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_1.GetLeaguesRequest));
this.registerMapping(dtos_1.IncidentsFilterDto, requests_1.IncidentsFilter, (sourceFilterDtoUntyped) => {
const sourceFilterDto = sourceFilterDtoUntyped;
const destinationFilter = new requests_1.IncidentsFilter();
destinationFilter.ids = sourceFilterDto.ids;
destinationFilter.sports = sourceFilterDto.sports;
destinationFilter.searchText = sourceFilterDto.searchText;
if (sourceFilterDto.from) {
destinationFilter.from = '2023-10-01 10:00:00';
}
return destinationFilter;
});
this.registerMapping(dtos_1.GetIncidentsRequestDto, requests_1.GetIncidentsRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_1.GetIncidentsRequest));
this.registerMapping(dtos_1.GetVenuesRequestDto, requests_1.GetVenuesRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_1.GetVenuesRequest));
this.registerMapping(dtos_1.GetCitiesRequestDto, requests_1.GetCitiesRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_1.GetCitiesRequest));
this.registerMapping(dtos_1.GetStatesRequestDto, requests_1.GetStatesRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_1.GetStatesRequest));
this.registerMapping(dtos_1.GetMarketsRequestDto, requests_1.GetMarketsRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_1.GetMarketsRequest));
this.registerMapping(dtos_1.GetTranslationsRequestDto, requests_1.GetTranslationsRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_1.GetTranslationsRequest));
this.registerMapping(dtos_1.GetCompetitionsRequestDto, requests_1.GetCompetitionsRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_1.GetCompetitionsRequest));
this.registerMapping(dtos_2.GetFixtureScheduleRequestDto, requests_2.GetFixtureScheduleRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_2.GetFixtureScheduleRequest));
this.registerMapping(dtos_2.FixturesSubscriptionRequestDto, requests_2.FixturesSubscriptionRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_2.FixturesSubscriptionRequest));
this.registerMapping(dtos_2.LeaguesSubscriptionRequestDto, requests_2.LeaguesSubscriptionRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_2.LeaguesSubscriptionRequest));
this.registerMapping(dtos_2.GetSubscriptionsRequestDto, requests_2.GetSubscriptionsRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_2.GetSubscriptionsRequest));
this.registerMapping(dtos_2.CompetitionsSubscriptionRequestDto, requests_2.CompetitionsSubscriptionRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_2.CompetitionsSubscriptionRequest));
this.registerMapping(dtos_2.ChangeManualSuspensionsRequestDto, requests_2.ChangeManualSuspensionsRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_2.ChangeManualSuspensionsRequest));
this.registerMapping(dtos_2.FixturesMetadataSubscriptionsRequestDto, requests_2.FixturesMetadataSubscriptionsRequest, (source) => {
let destination = {
fromDate: this.formatDate('fromDate', source.fromDate),
toDate: this.formatDate('toDate', source.toDate),
};
destination = _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...destination }, requests_2.FixturesMetadataSubscriptionsRequest);
return destination;
});
this.registerMapping(dtos_1.GetParticipantsRequestDto, requests_1.GetParticipantsRequest, (source) => _utilities_1.TransformerUtil.transform({ ...packageCredentials, ...source }, requests_1.GetParticipantsRequest));
this.registerMapping(dtos_1.ParticipantFilterDto, requests_1.ParticipantsFilter, (source) => _utilities_1.TransformerUtil.transform(source, requests_1.ParticipantsFilter));
this.registerMapping(dtos_1.VenueFilterDto, requests_1.VenuesFilter, (source) => _utilities_1.TransformerUtil.transform(source, requests_1.VenuesFilter));
this.registerMapping(dtos_1.CityFilterDto, requests_1.CitiesFilter, (source) => _utilities_1.TransformerUtil.transform(source, requests_1.CitiesFilter));
this.registerMapping(dtos_1.StateFilterDto, requests_1.StatesFilter, (source) => _utilities_1.TransformerUtil.transform(source, requests_1.StatesFilter));
}
/**
* Registers a new mapping configuration between two types
* of objects in the application using a mapping function
* to transform the source object to the destination object
* type specified in the mapping configuration.
* @param sourceType The constructor of the source type
* @param destinationType The constructor of the destination
* type to map to the source type using the mapping function
* provided in the configuration profile for the mapping
* between the two types of objects
* @param mappingFn The function that performs the mapping
* from source to destination type objects in the application
* using the mapping function provided in the configuration
* profile for the mapping between the two types of objects
* in the application using the mapping function provided in
* the configuration profile for the mapping.
* @returns void to indicate that the mapping configuration
* has been registered successfully in the application.
*/
registerMapping(sourceType, destinationType, mappingFn) {
const key = `${sourceType.name}-${destinationType.name}`;
this.mappingConfigs.set(key, mappingFn);
}
/**
* Formats a date to MM/DD/YYYY string format
* @param fieldName The name of the date field
* to format.
* @param date The date to format to MM/DD/YYYY string
* format.
* @returns Formatted date string in MM/DD/YYYY format
*/
formatDate(fieldName, date) {
if ((0, lodash_1.isNil)(date)) {
throw new _entities_1.InvalidDateInRequestError(fieldName);
}
return date.format(this.desiredDateFormat);
}
}
exports.Mapper = Mapper;
//# sourceMappingURL=mapper.js.map