Skip to content

Commit

Permalink
FIX: Expose ApiCoreNotificationCenter thru function
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred78290 committed Nov 7, 2023
1 parent 8859d90 commit 45a01ab
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 156 deletions.
159 changes: 7 additions & 152 deletions src/apicore/api.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,10 @@
import ApiCoreLiveReport from './livereport'
import ApiCoreNotificationCenter from './notification'
import defaultSearchParams from '../default-search-params'
import { ApiCoreResponseDocuments, ApiCoreResponseTopics, ClientCredentials, Lang, Params, Query, Request, Token, SortField, SortOrder, ApiCoreDocument } from '../types'
import buildQuery from '../utils/query-builder'
import buildQueryFromParams from '../utils/parametizer'
import { ApiCoreResponseDocuments, ApiCoreResponseTopics, ClientCredentials, Lang, Params, Token, SortField, SortOrder, ApiCoreDocument } from '../types'
import { get, gettext, post } from '../utils/request'

const mapSearchParams = new Map([
['advisories', 'advisory'],
['afpshortids', 'afpshortid'],
['associatedWiths', 'associatedWith'],
['bagUnos', 'bagUno'],
['channels', 'channel'],
['cities', 'city'],
['citycodes', 'citycode'],
['classes', 'class'],
['comments', 'comment'],
['contains_videos', 'contains_video'],
['contributors', 'contributor'],
['countries', 'country'],
['country_onlys', 'country_only'],
['country_outs', 'country_out'],
['countrycodes', 'countrycode'],
['countrynames', 'countryname'],
['creators', 'creator'],
['depts', 'dept'],
['entity_companies', 'entity_company'],
['entity_departements', 'entity_departement'],
['entity_events', 'entity_event'],
['entity_functions', 'entity_function'],
['entity_locations', 'entity_location'],
['entity_medias', 'entity_media'],
['entity_organisations', 'entity_organisation'],
['entity_persons', 'entity_person'],
['entity_regions', 'entity_region'],
['entity_templates', 'entity_template'],
['entity_themes', 'entity_theme'],
['entity_works', 'entity_work'],
['events', 'event'],
['genres', 'genre'],
['genreids', 'genreid'],
['guids', 'guid'],
['headlines', 'headline'],
['hrefs', 'href'],
['introduceds', 'introduced'],
['iptcs', 'iptc'],
['keywords', 'keyword'],
['livereportids', 'livereportid'],
['mediatopics', 'mediatopic'],
['montageTypes', 'montageType'],
['news', 'news'],
['newsItemIDs', 'newsItemID'],
['objectNames', 'objectName'],
['ofinterestofs', 'ofinterestof'],
['products', 'product'],
['providers', 'provider'],
['providerids', 'providerid.qcode'],
['publicIdentifiers', 'publicIdentifier'],
['regions', 'region'],
['revisions', 'revision'],
['rules', 'rule'],
['scripts', 'script'],
['serials', 'serial'],
['signals', 'signal'],
['slugs', 'slug'],
['sources', 'source'],
['status', 'status'],
['subheadlines', 'subheadline'],
['thematics', 'thematic'],
['titles', 'title'],
['topics', 'topic'],
['unos', 'uno'],
['urgencies', 'urgency']
])

interface ApiCoreSocialStoryResponse {
expires: string
clientID: string
Expand All @@ -86,88 +19,6 @@ interface ApiCoreSocialStoryResponse {
queryID: string
}

export function buildQueryFromParams (defaultSearchParams: Params, searchOptions: {
params?: Params | null
options?: {
maxRows?: number
sortField?: SortField
sortOrder?: SortOrder
fields?: string[]
}
} = {}): Query {
const {
dateFrom,
dateTo,
query,
langs
} = Object.assign({}, defaultSearchParams, searchOptions.params)

const optionnalParams: any = {}
const optionnalRequest: [any?] = []
const search = Object.assign({}, defaultSearchParams, searchOptions.params)

if (searchOptions.params?.native) {
optionnalRequest.push(searchOptions.params?.native)
}

for (const [key, value] of Object.entries(search)) {
if (mapSearchParams.has(key)) {
const field = mapSearchParams.get(key)

if (value) {
if (Array.isArray(value)) {
if (value.length === 1) {
optionnalRequest.push({
value: value[0],
name: field
})
} else if (value.length > 1) {
optionnalRequest.push({
in: value,
name: field
})
}
} else {
optionnalRequest.push({
value: value,
name: field
})
}
}
}
}

if (langs && langs.length > 0) {
if (langs.length === 1) {
optionnalParams.lang = langs[0]
} else {
optionnalRequest.push({
in: langs,
name: 'lang'
})
}
}

const request: Request = {
and: [
...optionnalRequest,
...buildQuery(query)
]
}

const result: Query = {
dateRange: {
from: dateFrom,
to: dateTo
},
query: request,
...optionnalParams,
...searchOptions.options
}

return result
}

export default class ApiCoreSearch extends ApiCoreLiveReport {

constructor (credentials: ClientCredentials & {
Expand Down Expand Up @@ -340,4 +191,8 @@ export default class ApiCoreSearch extends ApiCoreLiveReport {

return null
}

public createNotificationCenter (): ApiCoreNotificationCenter {
return new ApiCoreNotificationCenter(this)
}
}
11 changes: 7 additions & 4 deletions src/apicore/notification.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ApiCoreAuth from './authentication'
import { buildQueryFromParams } from './api'
import buildQueryFromParams from '../utils/parametizer'
import { Subscription, RegisteredSubscription, SubscriptionsIdentifier, RegisteredService, RegisterService, SubscriptionsInService, Params, QuietTime } from '../types'
import defaultSearchParams from '../default-search-params'
import defaultSubscriptionParams from '../default-notification-params'
import { HttpHeaders, get, post, del } from '../utils/request'
import moment from 'moment-timezone'
Expand Down Expand Up @@ -59,7 +58,7 @@ type AddNotificationSubscriptionResponse = CommonNotificationCenterResponse
type DeleteNotificationSubscriptionResponse = CommonNotificationCenterResponse
type SetQuietTimeResponse = CommonNotificationCenterResponse

export default class ApiCoreNotificationCenter {
export class ApiCoreNotificationCenter {
private auth: ApiCoreAuth

constructor (auth: ApiCoreAuth) {
Expand Down Expand Up @@ -93,7 +92,9 @@ export default class ApiCoreNotificationCenter {
}

get defaultSearchParams (): Params {
return defaultSearchParams as Params
return {
langs: []
}
}

get baseUrl (): string {
Expand Down Expand Up @@ -311,3 +312,5 @@ export default class ApiCoreNotificationCenter {
return result
}
}

export default ApiCoreNotificationCenter
157 changes: 157 additions & 0 deletions src/utils/parametizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { Params, Query, Request, SortField, SortOrder } from '../types'
import buildQuery from '../utils/query-builder'

const mapSearchParams = new Map([
['advisories', 'advisory'],
['afpshortids', 'afpshortid'],
['associatedWiths', 'associatedWith'],
['bagUnos', 'bagUno'],
['channels', 'channel'],
['cities', 'city'],
['citycodes', 'citycode'],
['classes', 'class'],
['comments', 'comment'],
['contains_videos', 'contains_video'],
['contributors', 'contributor'],
['countries', 'country'],
['country_onlys', 'country_only'],
['country_outs', 'country_out'],
['countrycodes', 'countrycode'],
['countrynames', 'countryname'],
['creators', 'creator'],
['depts', 'dept'],
['entity_companies', 'entity_company'],
['entity_departements', 'entity_departement'],
['entity_events', 'entity_event'],
['entity_functions', 'entity_function'],
['entity_locations', 'entity_location'],
['entity_medias', 'entity_media'],
['entity_organisations', 'entity_organisation'],
['entity_persons', 'entity_person'],
['entity_regions', 'entity_region'],
['entity_templates', 'entity_template'],
['entity_themes', 'entity_theme'],
['entity_works', 'entity_work'],
['events', 'event'],
['genres', 'genre'],
['genreids', 'genreid'],
['guids', 'guid'],
['headlines', 'headline'],
['hrefs', 'href'],
['introduceds', 'introduced'],
['iptcs', 'iptc'],
['keywords', 'keyword'],
['livereportids', 'livereportid'],
['mediatopics', 'mediatopic'],
['montageTypes', 'montageType'],
['news', 'news'],
['newsItemIDs', 'newsItemID'],
['objectNames', 'objectName'],
['ofinterestofs', 'ofinterestof'],
['products', 'product'],
['providers', 'provider'],
['providerids', 'providerid.qcode'],
['publicIdentifiers', 'publicIdentifier'],
['regions', 'region'],
['revisions', 'revision'],
['rules', 'rule'],
['scripts', 'script'],
['serials', 'serial'],
['signals', 'signal'],
['slugs', 'slug'],
['sources', 'source'],
['status', 'status'],
['subheadlines', 'subheadline'],
['thematics', 'thematic'],
['titles', 'title'],
['topics', 'topic'],
['unos', 'uno'],
['urgencies', 'urgency']
])

function appendParams (search: Params, optionnalRequest: [any?]) {
for (const [key, value] of Object.entries(search)) {
if (mapSearchParams.has(key)) {
const field = mapSearchParams.get(key)

if (value) {
if (Array.isArray(value)) {
if (value.length === 1) {
optionnalRequest.push({
value: value[0],
name: field
})
} else if (value.length > 1) {
optionnalRequest.push({
in: value,
name: field
})
}
} else {
optionnalRequest.push({
value: value,
name: field
})
}
}
}
}
}

export default function buildQueryFromParams (defaultSearchParams: Params, searchOptions: {
params?: Params | null
options?: {
maxRows?: number
sortField?: SortField
sortOrder?: SortOrder
fields?: string[]
}
} = {}): Query {
const {
dateFrom,
dateTo,
query,
langs
} = Object.assign({}, defaultSearchParams, searchOptions.params)

const optionnalParams: any = {}
const optionnalRequest: [any?] = []
const search = Object.assign({}, defaultSearchParams, searchOptions.params)

if (searchOptions.params?.native) {
optionnalRequest.push(searchOptions.params?.native)
}

appendParams(search, optionnalRequest)

if (langs && langs.length > 0) {
if (langs.length === 1) {
optionnalParams.lang = langs[0]
} else {
optionnalRequest.push({
in: langs,
name: 'lang'
})
}
}

const request: Request = {
and: [
...optionnalRequest,
...buildQuery(query)
]
}

const result: Query = {
dateRange: {
from: dateFrom,
to: dateTo
},
query: request,
...optionnalParams,
...searchOptions.options
}

return result
}

0 comments on commit 45a01ab

Please sign in to comment.