{"version":3,"file":"index.CzFPzMrV.js","sources":["../../../../../../src/lib/utils/event-utils/index.ts"],"sourcesContent":["import { PUBLIC_CONTENTFUL_ENVIRONMENT } from '$env/static/public';\nimport type { IInitiatives } from \"$lib/utils/contentful/types\";\nimport type { InitiativeResults } from './types';\n\n/**\n * \n * https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters\n * \n **/\nconst conditions: { [key: string]: string } = {\n '==': '', // Equality operator\n '!=': '[ne]', // Inequality operator\n 'in': '[in]', // Inclusion\n 'not in': '[nin]', // Exclusion\n 'exists': '[exists]', // Existence\n '<': '[lt]', // Less than.\n '<=': '[lte]', // Less than or equal to.\n '>': '[gt]', // Greater than.\n '>=': '[gte]', // Greater than or equal to.\n}\n\n/**\n * Get the event utils object\n * @param _fetch The fetch function\n * @returns The event utils object\n */\nexport const useEventUtils = (_fetch: {\n (input: RequestInfo | URL, init?: RequestInit): Promise;\n (input: string | URL | globalThis.Request, init?: RequestInit): Promise;\n}): EventUtils => {\n return new EventUtils(_fetch);\n};\n\n// TODO: use contentful client injecting right fetch function\nexport class EventUtils {\n private static BASE_URI = 'https://cdn.contentful.com';\n private static SPACE_ID = 'fumyn2q16o4i';\n private static ACCESS_TOKEN = 'TRsNY4r-LgKqoW_EwZyjh0wI2Go0WxpIuV5xlsTpdHg';\n private static ENVIRONMENT_ID = PUBLIC_CONTENTFUL_ENVIRONMENT;\n private static CONTENT_TYPE = 'initiatives';\n\n private static PREVIEW_BASE_URI = 'https://preview.contentful.com';\n private static PREVIEW_ACCESS_TOKEN = 'NkKR_ZRrbcUcvvbuz3iXe8ZRpjp-PtV2Kvs7pQB9qx4';\n\n private _fetch: {\n (input: RequestInfo | URL, init?: RequestInit): Promise;\n (input: string | URL | globalThis.Request, init?: RequestInit): Promise;\n }\n\n public constructor(fetch: {\n (input: RequestInfo | URL, init?: RequestInit): Promise;\n (input: string | URL | globalThis.Request, init?: RequestInit): Promise;\n }) {\n this._fetch = fetch;\n }\n\n private fetchInitiative = async (url: string): Promise => {\n try {\n const res = await this._fetch(url)\n if (res.ok) {\n console.info(`Initiative at ${url} found.`);\n return await res.json();\n }\n\n console.warn(`[!] Initiative at ${url} not found.`, await res.text());\n\n return null;\n } catch (e) {\n console.warn(`[!] Initiative at ${url} not found.`, e);\n return null;\n }\n }\n\n public getInitiativeBySlug = async (\n slug: string,\n preview: boolean = false\n ): Promise => {\n if (preview) {\n const query = `${EventUtils.PREVIEW_BASE_URI}/spaces/${EventUtils.SPACE_ID}/environments/${EventUtils.ENVIRONMENT_ID}/entries?access_token=${EventUtils.PREVIEW_ACCESS_TOKEN}&content_type=${EventUtils.CONTENT_TYPE}&fields.slug=${slug}`;\n\n const rawCollection = await this._fetch(query).then((res) => res.json());\n\n if (rawCollection.items.length !== 0) {\n const entriesCollection = EventUtils.resolveLinks(rawCollection);\n return entriesCollection.items[0];\n } else {\n return null;\n }\n } else {\n return this.fetchInitiative(`/api/v1/initiatives/sl/${slug}.json`);\n }\n };\n\n public getInitiativeById = async (\n id: string\n ): Promise => {\n return this.fetchInitiative(`/api/v1/initiatives/id/${id}.json`);\n };\n\n public static initiativeTypeEvent = (type: string) => {\n return type == 'Evento Fisico' ? 'Evento in presenza' : type;\n };\n\n public getNextInitiatives = async (nextLimit: number, nextSkip: number, filters: string[] = [], order: string | null = null) => {\n let nextQuery = `${EventUtils.BASE_URI}/spaces/${EventUtils.SPACE_ID}/environments/${EventUtils.ENVIRONMENT_ID}/entries?access_token=${EventUtils.ACCESS_TOKEN}&content_type=${EventUtils.CONTENT_TYPE}&limit=${nextLimit}&skip=${nextSkip}`;\n\n if (order) {\n nextQuery += `&order=${order}`;\n }\n\n if (filters.length !== 0) {\n nextQuery += `&fields.type[in]=${filters.join(',')}`;\n }\n\n var nextRawCollection = await this._fetch(nextQuery).then((res) => res.json());\n\n return nextRawCollection;\n };\n\n public getCloserInitiatives = async (\n page: number = 0,\n pageSize: number = 20,\n filters: string[] = []\n ): Promise => {\n const { initiatives, total } = await this.getInitiatives(\n page,\n pageSize,\n filters,\n 'fields.date',\n [\n {\n field: 'date',\n condition: '>=',\n value: new Date(new Date().setUTCHours(0, 0, 0, 0)).toISOString()\n },\n {\n field: 'hideInitiativeFromWebsiteAndSearch',\n condition: '!=',\n value: 'true'\n }\n ]\n );\n\n return { initiatives, total };\n };\n\n public getPastInitiatives = async (\n page: number = 0,\n pageSize: number = 20,\n filters: string[] = []\n ): Promise => {\n const { initiatives, total } = await this.getInitiatives(\n page,\n pageSize,\n filters,\n '-fields.date',\n [\n {\n field: 'date',\n condition: '<',\n value: new Date(new Date().setUTCHours(0, 0, 0, 0)).toISOString()\n },\n {\n field: 'hideInitiativeFromWebsiteAndSearch',\n condition: '!=',\n value: 'true'\n }\n ]\n );\n\n return { initiatives, total };\n };\n\n public getInitiatives = async (\n page: number = 0,\n pageSize: number = 20,\n filters: string[] = [],\n order: string | null = null,\n customFilters: {\n field: string,\n condition: string,\n value: string\n }[] = []\n ): Promise => {\n const limit = pageSize;\n const skip = page * pageSize;\n\n // TODO: Add filters, remove direct contentful fetch and use static API\n\n let query = `${EventUtils.BASE_URI}/spaces/${EventUtils.SPACE_ID}/environments/${EventUtils.ENVIRONMENT_ID}/entries?access_token=${EventUtils.ACCESS_TOKEN}&content_type=${EventUtils.CONTENT_TYPE}&limit=${limit}&skip=${skip}`;\n\n if (order) {\n query += `&order=${order}`;\n }\n\n if (filters.length !== 0) {\n query += `&fields.type[in]=${filters.join(',')}`;\n }\n\n if (customFilters.length !== 0) {\n customFilters.forEach(filter => {\n query += `&fields.${filter.field}${conditions[filter.condition]}=${filter.value}`;\n });\n }\n\n var rawCollection = await this._fetch(query).then((res) => res.json());\n\n var entriesCollection = EventUtils.resolveLinks(rawCollection);\n var initiatives = [...entriesCollection.items];\n\n return {\n initiatives: initiatives,\n total: entriesCollection.total,\n };\n };\n\n private static resolveLinks(entriesCollection) {\n const copy = { ...entriesCollection };\n\n if (entriesCollection.includes && entriesCollection.includes.Asset) {\n const assets = entriesCollection.includes.Asset;\n copy.items.forEach((item) => {\n const fields = item.fields;\n for (const field in fields) {\n if (fields[field].sys && fields[field].sys.linkType === 'Asset') {\n const linkId = fields[field].sys.id;\n const linkedAsset = assets.find((asset) => asset.sys.id === linkId);\n fields[field] = { ...linkedAsset };\n }\n }\n });\n delete copy.includes;\n return copy;\n }\n return entriesCollection;\n }\n}\n"],"names":["conditions","useEventUtils","_fetch","EventUtils","_EventUtils","fetch","__publicField","url","res","slug","preview","query","rawCollection","id","nextLimit","nextSkip","filters","order","nextQuery","nextRawCollection","page","pageSize","initiatives","total","customFilters","limit","skip","filter","entriesCollection","copy","assets","item","fields","field","linkId","linkedAsset","asset","PUBLIC_CONTENTFUL_ENVIRONMENT","type"],"mappings":"qNASA,MAAMA,EAAwC,CAC5C,KAAM,GACN,KAAM,OACN,GAAM,OACN,SAAU,QACV,OAAU,WACV,IAAK,OACL,KAAM,QACN,IAAK,OACL,KAAM,OACR,EAOaC,EAAiBC,GAIrB,IAAIC,EAAWD,CAAM,EAIjBE,EAAN,MAAMA,CAAW,CAef,YAAYC,EAGhB,CARKC,EAAA,eAYAA,EAAA,uBAAkB,MAAOC,GAA8C,CACzE,GAAA,CACF,MAAMC,EAAM,MAAM,KAAK,OAAOD,CAAG,EACjC,OAAIC,EAAI,IACE,QAAA,KAAK,iBAAiBD,CAAG,SAAS,EACnC,MAAMC,EAAI,KAAK,IAGxB,QAAQ,KAAK,qBAAqBD,CAAG,cAAe,MAAMC,EAAI,MAAM,EAE7D,YACA,EAAG,CACV,eAAQ,KAAK,qBAAqBD,CAAG,cAAe,CAAC,EAC9C,IAAA,CAEX,GAEOD,EAAA,2BAAsB,MAC3BG,EACAC,EAAmB,KACc,CACjC,GAAIA,EAAS,CACX,MAAMC,EAAQ,GAAGP,EAAW,gBAAgB,WAAWA,EAAW,QAAQ,iBAAiBA,EAAW,cAAc,yBAAyBA,EAAW,oBAAoB,iBAAiBA,EAAW,YAAY,gBAAgBK,CAAI,GAElOG,EAAgB,M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