@infineit-nestjs/services 1.0.12 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/services/index.d.ts +2 -0
- package/dist/cjs/services/index.js +1 -1
- package/dist/cjs/services/pagination.service.d.ts +7 -0
- package/dist/cjs/services/pagination.service.js +1 -0
- package/dist/cjs/services/resource.fetch.service.d.ts +54 -0
- package/dist/cjs/services/resource.fetch.service.js +1 -0
- package/dist/es/index.d.ts +2 -0
- package/dist/es/index.js +1 -1
- package/dist/es/services/index.d.ts +2 -0
- package/dist/es/services/index.js +1 -1
- package/dist/es/services/pagination.service.d.ts +7 -0
- package/dist/es/services/pagination.service.js +1 -0
- package/dist/es/services/resource.fetch.service.d.ts +54 -0
- package/dist/es/services/resource.fetch.service.js +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/services/index.d.ts +2 -0
- package/dist/types/services/pagination.service.d.ts +10 -0
- package/dist/types/services/resource.fetch.service.d.ts +58 -0
- package/package.json +5 -3
package/dist/cjs/index.d.ts
CHANGED
|
@@ -5,5 +5,7 @@ export * from './services/http.client.service';
|
|
|
5
5
|
export * from './services/logger.service';
|
|
6
6
|
export * from './services/message.formatter.service';
|
|
7
7
|
export * from './services/merge.service';
|
|
8
|
+
export * from './services/pagination.service';
|
|
8
9
|
export * from './services/prisma.service';
|
|
10
|
+
export * from './services/resource.fetch.service';
|
|
9
11
|
export * from './services/validator.service';
|
package/dist/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./helper.module"),exports),e.__exportStar(require("./services/argon2.service"),exports),e.__exportStar(require("./services/data.transformer.service"),exports),e.__exportStar(require("./services/http.client.service"),exports),e.__exportStar(require("./services/logger.service"),exports),e.__exportStar(require("./services/message.formatter.service"),exports),e.__exportStar(require("./services/merge.service"),exports),e.__exportStar(require("./services/prisma.service"),exports),e.__exportStar(require("./services/validator.service"),exports);
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./helper.module"),exports),e.__exportStar(require("./services/argon2.service"),exports),e.__exportStar(require("./services/data.transformer.service"),exports),e.__exportStar(require("./services/http.client.service"),exports),e.__exportStar(require("./services/logger.service"),exports),e.__exportStar(require("./services/message.formatter.service"),exports),e.__exportStar(require("./services/merge.service"),exports),e.__exportStar(require("./services/pagination.service"),exports),e.__exportStar(require("./services/prisma.service"),exports),e.__exportStar(require("./services/resource.fetch.service"),exports),e.__exportStar(require("./services/validator.service"),exports);
|
|
@@ -4,5 +4,7 @@ export * from './http.client.service';
|
|
|
4
4
|
export * from './logger.service';
|
|
5
5
|
export * from './message.formatter.service';
|
|
6
6
|
export * from './merge.service';
|
|
7
|
+
export * from './pagination.service';
|
|
7
8
|
export * from './prisma.service';
|
|
9
|
+
export * from './resource.fetch.service';
|
|
8
10
|
export * from './validator.service';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./argon2.service"),exports),e.__exportStar(require("./data.transformer.service"),exports),e.__exportStar(require("./http.client.service"),exports),e.__exportStar(require("./logger.service"),exports),e.__exportStar(require("./message.formatter.service"),exports),e.__exportStar(require("./merge.service"),exports),e.__exportStar(require("./prisma.service"),exports),e.__exportStar(require("./validator.service"),exports);
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./argon2.service"),exports),e.__exportStar(require("./data.transformer.service"),exports),e.__exportStar(require("./http.client.service"),exports),e.__exportStar(require("./logger.service"),exports),e.__exportStar(require("./message.formatter.service"),exports),e.__exportStar(require("./merge.service"),exports),e.__exportStar(require("./pagination.service"),exports),e.__exportStar(require("./prisma.service"),exports),e.__exportStar(require("./resource.fetch.service"),exports),e.__exportStar(require("./validator.service"),exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PaginationResponseDto, PaginationQueryDto } from '@infineit-nestjs/utils/dto';
|
|
2
|
+
import { PrismaService } from './prisma.service';
|
|
3
|
+
export declare class PaginationService {
|
|
4
|
+
private readonly prisma;
|
|
5
|
+
constructor(prisma: PrismaService);
|
|
6
|
+
paginate<T>(selectQuery: string, countQuery: string, paginationQuery: PaginationQueryDto): Promise<PaginationResponseDto<T>>;
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PaginationService=void 0;const e=require("tslib"),t=require("@nestjs/common"),i=require("@infineit-nestjs/utils/dto"),a=require("./prisma.service");let n=class{constructor(e){this.prisma=e}async paginate(e,t,a){const{page:n=1,limit:r=10,pagination:s}=a,o=`${e} LIMIT ${r} OFFSET ${(n-1)*r}`,c="all"===s||"paginate"===s,[p,l]=await Promise.all([this.prisma.$queryRawUnsafe(o),c?this.prisma.$queryRawUnsafe(t):[{count:0}]]);return{data:p,meta:c?new i.PaginationMetaDto({paginationQueryDto:a,totalItems:Number(l[0].count)}):void 0,has_next_page:"all"===s||"infinity"===s?p.length===r:void 0}}};exports.PaginationService=n,exports.PaginationService=n=e.__decorate([(0,t.Injectable)(),e.__metadata("design:paramtypes",[a.PrismaService])],n);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { IFilter, ISort } from '@infineit-nestjs/types/interfaces';
|
|
2
|
+
import { DataTransformerService } from './data.transformer.service';
|
|
3
|
+
import { HttpServiceClient } from './http.client.service';
|
|
4
|
+
export interface IResourceConfig {
|
|
5
|
+
resource: string;
|
|
6
|
+
type: string;
|
|
7
|
+
filters?: IFilter[];
|
|
8
|
+
limit?: number;
|
|
9
|
+
page?: number;
|
|
10
|
+
version?: string;
|
|
11
|
+
sort?: ISort[];
|
|
12
|
+
pagination?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare class ResourceFetchService {
|
|
15
|
+
private readonly dataTransformerService;
|
|
16
|
+
private readonly httpServiceClient;
|
|
17
|
+
private readonly logger;
|
|
18
|
+
constructor(dataTransformerService: DataTransformerService, httpServiceClient: HttpServiceClient);
|
|
19
|
+
fetchResourcesDynamic(resourceTypes: IResourceConfig[]): Promise<Record<string, any>>;
|
|
20
|
+
transformMultipleDataSets(dataSets: {
|
|
21
|
+
name: string;
|
|
22
|
+
data: any[] | undefined;
|
|
23
|
+
fields: string[];
|
|
24
|
+
}[]): Promise<Record<string, any>>;
|
|
25
|
+
getUniqueFieldValues<T>(data: T[], fields: string[]): Promise<Record<string, any[]>>;
|
|
26
|
+
mergeData<T>(addressData: T[], transformedData: {
|
|
27
|
+
field: string;
|
|
28
|
+
data: any[];
|
|
29
|
+
field_name: string;
|
|
30
|
+
removeField?: boolean;
|
|
31
|
+
}[]): Promise<any>;
|
|
32
|
+
generateQueryFunctions(queryMap: {
|
|
33
|
+
field: string;
|
|
34
|
+
query: Function;
|
|
35
|
+
}[], executeRawQuery: Function): Promise<Record<string, (ids: string) => Promise<any[]>>>;
|
|
36
|
+
applyDataTransformations<T>(data: T[], fields: string[], queries: Record<string, (ids: string) => Promise<any[]>>, mappings: {
|
|
37
|
+
field: string;
|
|
38
|
+
field_name: string;
|
|
39
|
+
removeField?: boolean;
|
|
40
|
+
}[]): Promise<any[]>;
|
|
41
|
+
fetchAssociatedData(distinctValues: Record<string, any[]>, queries: Record<string, (ids: string) => Promise<any[]>>): Promise<Record<string, any[]>>;
|
|
42
|
+
private mapDataForTransformation;
|
|
43
|
+
mergeEnrichedData<T>(mergeData: T[], transformations: {
|
|
44
|
+
field: string;
|
|
45
|
+
data: any[];
|
|
46
|
+
field_name: string;
|
|
47
|
+
removeField?: boolean;
|
|
48
|
+
}[]): Promise<any[]>;
|
|
49
|
+
private constructRelativePath;
|
|
50
|
+
private constructFilters;
|
|
51
|
+
private constructSort;
|
|
52
|
+
private fetchResources;
|
|
53
|
+
private getFieldValue;
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e;Object.defineProperty(exports,"__esModule",{value:!0}),exports.ResourceFetchService=void 0;const r=require("tslib"),t=require("@nestjs/common"),a=require("@infineit-nestjs/core/errors"),i=require("./data.transformer.service"),s=require("./http.client.service");let n=e=class{constructor(r,a){this.dataTransformerService=r,this.httpServiceClient=a,this.logger=new t.Logger(e.name)}async fetchResourcesDynamic(e){const r=e.map((e=>{const{resource:r,type:t,page:a=1,limit:i=10,version:s="v1",filters:n=[],sort:o=[],pagination:c="all"}=e,l=this.constructRelativePath({resource:r,type:t,page:a,limit:i,version:s,filters:n,sort:o,pagination:c});return this.fetchResources(r,l).then((e=>({[t]:e}))).catch((e=>(this.logger.error(`Error fetching resource ${t}: ${e.message}`),{[t]:{error:e.message}})))}));return(await Promise.all(r)).reduce(((e,r)=>Object.assign(e,r)),{})}async transformMultipleDataSets(e){const r=e.map((({name:e,data:r,fields:t})=>r?this.dataTransformerService.processData(r,t).then((r=>({[e]:r}))):Promise.resolve({[e]:{error:`${e} data is undefined`}})));return(await Promise.all(r)).reduce(((e,r)=>Object.assign(e,r)),{})}async getUniqueFieldValues(e,r){return r.reduce(((r,t)=>(r[t]=[...new Set(e.map((e=>e[t])))],r)),{})}async mergeData(e,r){return e.map((e=>r.reduce(((r,{field:t,data:a,field_name:i,removeField:s})=>{const n=this.getFieldValue(a,t,e[t],{[t]:e[t],name:`Default ${t.charAt(0).toUpperCase()+t.slice(1)}`});return r[i]=n,s&&delete r[t],r}),{...e})))}async generateQueryFunctions(e,r){return e.reduce(((e,{field:t,query:a})=>(e[t]=async e=>{const t=await r(a,e);return Array.isArray(t)?t:[t]},e)),{})}async applyDataTransformations(e,r,t,a){const i=await this.getUniqueFieldValues(e,r),s=await this.fetchAssociatedData(i,t),n=this.mapDataForTransformation(s,a);return this.mergeEnrichedData(e,n)}async fetchAssociatedData(e,r){const t={};return await Promise.all(Object.entries(r).map((async([r,i])=>{const s=e[r];if(!Array.isArray(s)||0===s.length)throw new a.MissingDataError(r);try{const e=await i(s.join(","));t[r]=Array.isArray(e)?e:[e]}catch(e){throw new Error(`Failed to fetch associated data for field: ${r}`,e)}}))),t}mapDataForTransformation(e,r){return r.map((({field:r,field_name:t,removeField:a})=>({field:r,data:e[r],field_name:t,removeField:a??!0})))}async mergeEnrichedData(e,r){const t=r.reduce(((e,{field:r,data:t,field_name:a})=>(e[a]=new Map(t.map((e=>[e[r],e]))),e)),{});return e.map((e=>{const a={...e};return r.forEach((({field:r,field_name:i,removeField:s})=>{const n=t[i],o=e[r],c=n?.get(o)||{[r]:o,name:`unknown-id-${o}`};a[i]=c,s&&delete a[r]})),a}))}constructRelativePath({resource:e,type:r,page:t,limit:a,version:i,filters:s,sort:n,pagination:o}){let c=`api/${i}/${r}`;const l=this.constructFilters(s),u=this.constructSort(n);return c+=`?pagination=${o}&page=${t}&limit=${a}`,l.length>0&&(c+=`&filters=[${l.join(",")}]`),u.length>0&&(c+=`&sort=[${u.join(",")}]`),this.logger.debug(`Constructed relative URL: ${c}`),c}constructFilters(e){return e.map((e=>`{"field":"${e.field}","operator":"${e.operator}","value":"${e.value}"}`))}constructSort(e){return e.map((e=>`{"field":"${e.field}","direction":"${e.direction}"}`))}fetchResources(e,r){return this.httpServiceClient.getResource(e,"get",r)}getFieldValue(e,r,t,a){return e.find((e=>e[r]===t))||a}};exports.ResourceFetchService=n,exports.ResourceFetchService=n=e=r.__decorate([(0,t.Injectable)(),r.__metadata("design:paramtypes",[i.DataTransformerService,s.HttpServiceClient])],n);
|
package/dist/es/index.d.ts
CHANGED
|
@@ -5,5 +5,7 @@ export * from './services/http.client.service';
|
|
|
5
5
|
export * from './services/logger.service';
|
|
6
6
|
export * from './services/message.formatter.service';
|
|
7
7
|
export * from './services/merge.service';
|
|
8
|
+
export * from './services/pagination.service';
|
|
8
9
|
export * from './services/prisma.service';
|
|
10
|
+
export * from './services/resource.fetch.service';
|
|
9
11
|
export * from './services/validator.service';
|
package/dist/es/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./helper.module"),exports),e.__exportStar(require("./services/argon2.service"),exports),e.__exportStar(require("./services/data.transformer.service"),exports),e.__exportStar(require("./services/http.client.service"),exports),e.__exportStar(require("./services/logger.service"),exports),e.__exportStar(require("./services/message.formatter.service"),exports),e.__exportStar(require("./services/merge.service"),exports),e.__exportStar(require("./services/prisma.service"),exports),e.__exportStar(require("./services/validator.service"),exports);
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./helper.module"),exports),e.__exportStar(require("./services/argon2.service"),exports),e.__exportStar(require("./services/data.transformer.service"),exports),e.__exportStar(require("./services/http.client.service"),exports),e.__exportStar(require("./services/logger.service"),exports),e.__exportStar(require("./services/message.formatter.service"),exports),e.__exportStar(require("./services/merge.service"),exports),e.__exportStar(require("./services/pagination.service"),exports),e.__exportStar(require("./services/prisma.service"),exports),e.__exportStar(require("./services/resource.fetch.service"),exports),e.__exportStar(require("./services/validator.service"),exports);
|
|
@@ -4,5 +4,7 @@ export * from './http.client.service';
|
|
|
4
4
|
export * from './logger.service';
|
|
5
5
|
export * from './message.formatter.service';
|
|
6
6
|
export * from './merge.service';
|
|
7
|
+
export * from './pagination.service';
|
|
7
8
|
export * from './prisma.service';
|
|
9
|
+
export * from './resource.fetch.service';
|
|
8
10
|
export * from './validator.service';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./argon2.service"),exports),e.__exportStar(require("./data.transformer.service"),exports),e.__exportStar(require("./http.client.service"),exports),e.__exportStar(require("./logger.service"),exports),e.__exportStar(require("./message.formatter.service"),exports),e.__exportStar(require("./merge.service"),exports),e.__exportStar(require("./prisma.service"),exports),e.__exportStar(require("./validator.service"),exports);
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});const e=require("tslib");e.__exportStar(require("./argon2.service"),exports),e.__exportStar(require("./data.transformer.service"),exports),e.__exportStar(require("./http.client.service"),exports),e.__exportStar(require("./logger.service"),exports),e.__exportStar(require("./message.formatter.service"),exports),e.__exportStar(require("./merge.service"),exports),e.__exportStar(require("./pagination.service"),exports),e.__exportStar(require("./prisma.service"),exports),e.__exportStar(require("./resource.fetch.service"),exports),e.__exportStar(require("./validator.service"),exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PaginationResponseDto, PaginationQueryDto } from '@infineit-nestjs/utils/dto';
|
|
2
|
+
import { PrismaService } from './prisma.service';
|
|
3
|
+
export declare class PaginationService {
|
|
4
|
+
private readonly prisma;
|
|
5
|
+
constructor(prisma: PrismaService);
|
|
6
|
+
paginate<T>(selectQuery: string, countQuery: string, paginationQuery: PaginationQueryDto): Promise<PaginationResponseDto<T>>;
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.PaginationService=void 0;const e=require("tslib"),t=require("@nestjs/common"),a=require("@infineit-nestjs/utils/dto"),i=require("./prisma.service");let n=class{constructor(e){this.prisma=e}async paginate(e,t,i){const{page:n=1,limit:r=10,pagination:s}=i,o=`${e} LIMIT ${r} OFFSET ${(n-1)*r}`,c="all"===s||"paginate"===s,[p,l]=await Promise.all([this.prisma.$queryRawUnsafe(o),c?this.prisma.$queryRawUnsafe(t):[{count:0}]]);return{data:p,meta:c?new a.PaginationMetaDto({paginationQueryDto:i,totalItems:Number(l[0].count)}):void 0,has_next_page:"all"===s||"infinity"===s?p.length===r:void 0}}};exports.PaginationService=n,exports.PaginationService=n=e.__decorate([(0,t.Injectable)(),e.__metadata("design:paramtypes",[i.PrismaService])],n);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { IFilter, ISort } from '@infineit-nestjs/types/interfaces';
|
|
2
|
+
import { DataTransformerService } from './data.transformer.service';
|
|
3
|
+
import { HttpServiceClient } from './http.client.service';
|
|
4
|
+
export interface IResourceConfig {
|
|
5
|
+
resource: string;
|
|
6
|
+
type: string;
|
|
7
|
+
filters?: IFilter[];
|
|
8
|
+
limit?: number;
|
|
9
|
+
page?: number;
|
|
10
|
+
version?: string;
|
|
11
|
+
sort?: ISort[];
|
|
12
|
+
pagination?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare class ResourceFetchService {
|
|
15
|
+
private readonly dataTransformerService;
|
|
16
|
+
private readonly httpServiceClient;
|
|
17
|
+
private readonly logger;
|
|
18
|
+
constructor(dataTransformerService: DataTransformerService, httpServiceClient: HttpServiceClient);
|
|
19
|
+
fetchResourcesDynamic(resourceTypes: IResourceConfig[]): Promise<Record<string, any>>;
|
|
20
|
+
transformMultipleDataSets(dataSets: {
|
|
21
|
+
name: string;
|
|
22
|
+
data: any[] | undefined;
|
|
23
|
+
fields: string[];
|
|
24
|
+
}[]): Promise<Record<string, any>>;
|
|
25
|
+
getUniqueFieldValues<T>(data: T[], fields: string[]): Promise<Record<string, any[]>>;
|
|
26
|
+
mergeData<T>(addressData: T[], transformedData: {
|
|
27
|
+
field: string;
|
|
28
|
+
data: any[];
|
|
29
|
+
field_name: string;
|
|
30
|
+
removeField?: boolean;
|
|
31
|
+
}[]): Promise<any>;
|
|
32
|
+
generateQueryFunctions(queryMap: {
|
|
33
|
+
field: string;
|
|
34
|
+
query: Function;
|
|
35
|
+
}[], executeRawQuery: Function): Promise<Record<string, (ids: string) => Promise<any[]>>>;
|
|
36
|
+
applyDataTransformations<T>(data: T[], fields: string[], queries: Record<string, (ids: string) => Promise<any[]>>, mappings: {
|
|
37
|
+
field: string;
|
|
38
|
+
field_name: string;
|
|
39
|
+
removeField?: boolean;
|
|
40
|
+
}[]): Promise<any[]>;
|
|
41
|
+
fetchAssociatedData(distinctValues: Record<string, any[]>, queries: Record<string, (ids: string) => Promise<any[]>>): Promise<Record<string, any[]>>;
|
|
42
|
+
private mapDataForTransformation;
|
|
43
|
+
mergeEnrichedData<T>(mergeData: T[], transformations: {
|
|
44
|
+
field: string;
|
|
45
|
+
data: any[];
|
|
46
|
+
field_name: string;
|
|
47
|
+
removeField?: boolean;
|
|
48
|
+
}[]): Promise<any[]>;
|
|
49
|
+
private constructRelativePath;
|
|
50
|
+
private constructFilters;
|
|
51
|
+
private constructSort;
|
|
52
|
+
private fetchResources;
|
|
53
|
+
private getFieldValue;
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e;Object.defineProperty(exports,"__esModule",{value:!0}),exports.ResourceFetchService=void 0;const r=require("tslib"),t=require("@nestjs/common"),a=require("@infineit-nestjs/core/errors"),i=require("./data.transformer.service"),s=require("./http.client.service");let n=e=class{constructor(r,a){this.dataTransformerService=r,this.httpServiceClient=a,this.logger=new t.Logger(e.name)}async fetchResourcesDynamic(e){const r=e.map((e=>{const{resource:r,type:t,page:a=1,limit:i=10,version:s="v1",filters:n=[],sort:o=[],pagination:c="all"}=e,l=this.constructRelativePath({resource:r,type:t,page:a,limit:i,version:s,filters:n,sort:o,pagination:c});return this.fetchResources(r,l).then((e=>({[t]:e}))).catch((e=>(this.logger.error(`Error fetching resource ${t}: ${e.message}`),{[t]:{error:e.message}})))}));return(await Promise.all(r)).reduce(((e,r)=>Object.assign(e,r)),{})}async transformMultipleDataSets(e){const r=e.map((({name:e,data:r,fields:t})=>r?this.dataTransformerService.processData(r,t).then((r=>({[e]:r}))):Promise.resolve({[e]:{error:`${e} data is undefined`}})));return(await Promise.all(r)).reduce(((e,r)=>Object.assign(e,r)),{})}async getUniqueFieldValues(e,r){return r.reduce(((r,t)=>(r[t]=[...new Set(e.map((e=>e[t])))],r)),{})}async mergeData(e,r){return e.map((e=>r.reduce(((r,{field:t,data:a,field_name:i,removeField:s})=>{const n=this.getFieldValue(a,t,e[t],{[t]:e[t],name:`Default ${t.charAt(0).toUpperCase()+t.slice(1)}`});return r[i]=n,s&&delete r[t],r}),{...e})))}async generateQueryFunctions(e,r){return e.reduce(((e,{field:t,query:a})=>(e[t]=async e=>{const t=await r(a,e);return Array.isArray(t)?t:[t]},e)),{})}async applyDataTransformations(e,r,t,a){const i=await this.getUniqueFieldValues(e,r),s=await this.fetchAssociatedData(i,t),n=this.mapDataForTransformation(s,a);return this.mergeEnrichedData(e,n)}async fetchAssociatedData(e,r){const t={};return await Promise.all(Object.entries(r).map((async([r,i])=>{const s=e[r];if(!Array.isArray(s)||0===s.length)throw new a.MissingDataError(r);try{const e=await i(s.join(","));t[r]=Array.isArray(e)?e:[e]}catch(e){throw new Error(`Failed to fetch associated data for field: ${r}`,e)}}))),t}mapDataForTransformation(e,r){return r.map((({field:r,field_name:t,removeField:a})=>({field:r,data:e[r],field_name:t,removeField:a??!0})))}async mergeEnrichedData(e,r){const t=r.reduce(((e,{field:r,data:t,field_name:a})=>(e[a]=new Map(t.map((e=>[e[r],e]))),e)),{});return e.map((e=>{const a={...e};return r.forEach((({field:r,field_name:i,removeField:s})=>{const n=t[i],o=e[r],c=n?.get(o)||{[r]:o,name:`unknown-id-${o}`};a[i]=c,s&&delete a[r]})),a}))}constructRelativePath({resource:e,type:r,page:t,limit:a,version:i,filters:s,sort:n,pagination:o}){let c=`api/${i}/${r}`;const l=this.constructFilters(s),u=this.constructSort(n);return c+=`?pagination=${o}&page=${t}&limit=${a}`,l.length>0&&(c+=`&filters=[${l.join(",")}]`),u.length>0&&(c+=`&sort=[${u.join(",")}]`),this.logger.debug(`Constructed relative URL: ${c}`),c}constructFilters(e){return e.map((e=>`{"field":"${e.field}","operator":"${e.operator}","value":"${e.value}"}`))}constructSort(e){return e.map((e=>`{"field":"${e.field}","direction":"${e.direction}"}`))}fetchResources(e,r){return this.httpServiceClient.getResource(e,"get",r)}getFieldValue(e,r,t,a){return e.find((e=>e[r]===t))||a}};exports.ResourceFetchService=n,exports.ResourceFetchService=n=e=r.__decorate([(0,t.Injectable)(),r.__metadata("design:paramtypes",[i.DataTransformerService,s.HttpServiceClient])],n);
|
package/dist/types/index.d.ts
CHANGED
|
@@ -5,5 +5,7 @@ export { HttpServiceClient } from './services/http.client.service.js';
|
|
|
5
5
|
export { ILogData, LogLevel, LoggerService } from './services/logger.service.js';
|
|
6
6
|
export { MessageFormatterService } from './services/message.formatter.service.js';
|
|
7
7
|
export { MergeService } from './services/merge.service.js';
|
|
8
|
+
export { PaginationService } from './services/pagination.service.js';
|
|
8
9
|
export { PrismaService } from './services/prisma.service.js';
|
|
10
|
+
export { IResourceConfig, ResourceFetchService } from './services/resource.fetch.service.js';
|
|
9
11
|
export { ValidatorService } from './services/validator.service.js';
|
|
@@ -4,5 +4,7 @@ export { HttpServiceClient } from './http.client.service.js';
|
|
|
4
4
|
export { ILogData, LogLevel, LoggerService } from './logger.service.js';
|
|
5
5
|
export { MessageFormatterService } from './message.formatter.service.js';
|
|
6
6
|
export { MergeService } from './merge.service.js';
|
|
7
|
+
export { PaginationService } from './pagination.service.js';
|
|
7
8
|
export { PrismaService } from './prisma.service.js';
|
|
9
|
+
export { IResourceConfig, ResourceFetchService } from './resource.fetch.service.js';
|
|
8
10
|
export { ValidatorService } from './validator.service.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PaginationQueryDto, PaginationResponseDto } from '@infineit-nestjs/utils/dto';
|
|
2
|
+
import { PrismaService } from './prisma.service.js';
|
|
3
|
+
|
|
4
|
+
declare class PaginationService {
|
|
5
|
+
private readonly prisma;
|
|
6
|
+
constructor(prisma: PrismaService);
|
|
7
|
+
paginate<T>(selectQuery: string, countQuery: string, paginationQuery: PaginationQueryDto): Promise<PaginationResponseDto<T>>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { PaginationService };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { IFilter, ISort } from '@infineit-nestjs/types/interfaces';
|
|
2
|
+
import { DataTransformerService } from './data.transformer.service.js';
|
|
3
|
+
import { HttpServiceClient } from './http.client.service.js';
|
|
4
|
+
|
|
5
|
+
interface IResourceConfig {
|
|
6
|
+
resource: string;
|
|
7
|
+
type: string;
|
|
8
|
+
filters?: IFilter[];
|
|
9
|
+
limit?: number;
|
|
10
|
+
page?: number;
|
|
11
|
+
version?: string;
|
|
12
|
+
sort?: ISort[];
|
|
13
|
+
pagination?: string;
|
|
14
|
+
}
|
|
15
|
+
declare class ResourceFetchService {
|
|
16
|
+
private readonly dataTransformerService;
|
|
17
|
+
private readonly httpServiceClient;
|
|
18
|
+
private readonly logger;
|
|
19
|
+
constructor(dataTransformerService: DataTransformerService, httpServiceClient: HttpServiceClient);
|
|
20
|
+
fetchResourcesDynamic(resourceTypes: IResourceConfig[]): Promise<Record<string, any>>;
|
|
21
|
+
transformMultipleDataSets(dataSets: {
|
|
22
|
+
name: string;
|
|
23
|
+
data: any[] | undefined;
|
|
24
|
+
fields: string[];
|
|
25
|
+
}[]): Promise<Record<string, any>>;
|
|
26
|
+
getUniqueFieldValues<T>(data: T[], fields: string[]): Promise<Record<string, any[]>>;
|
|
27
|
+
mergeData<T>(addressData: T[], transformedData: {
|
|
28
|
+
field: string;
|
|
29
|
+
data: any[];
|
|
30
|
+
field_name: string;
|
|
31
|
+
removeField?: boolean;
|
|
32
|
+
}[]): Promise<any>;
|
|
33
|
+
generateQueryFunctions(queryMap: {
|
|
34
|
+
field: string;
|
|
35
|
+
query: Function;
|
|
36
|
+
}[], executeRawQuery: Function): Promise<Record<string, (ids: string) => Promise<any[]>>>;
|
|
37
|
+
applyDataTransformations<T>(data: T[], fields: string[], queries: Record<string, (ids: string) => Promise<any[]>>, mappings: {
|
|
38
|
+
field: string;
|
|
39
|
+
field_name: string;
|
|
40
|
+
removeField?: boolean;
|
|
41
|
+
}[]): Promise<any[]>;
|
|
42
|
+
fetchAssociatedData(distinctValues: Record<string, any[]>, queries: Record<string, (ids: string) => Promise<any[]>>): Promise<Record<string, any[]>>;
|
|
43
|
+
private mapDataForTransformation;
|
|
44
|
+
mergeEnrichedData<T>(mergeData: T[], transformations: {
|
|
45
|
+
field: string;
|
|
46
|
+
data: any[];
|
|
47
|
+
field_name: string;
|
|
48
|
+
removeField?: boolean;
|
|
49
|
+
}[]): Promise<any[]>;
|
|
50
|
+
private constructRelativePath;
|
|
51
|
+
private constructFilters;
|
|
52
|
+
private constructSort;
|
|
53
|
+
private fetchResources;
|
|
54
|
+
private getFieldValue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { ResourceFetchService };
|
|
58
|
+
export type { IResourceConfig };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infineit-nestjs/services",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"main": "./dist/cjs/index.js",
|
|
5
5
|
"module": "./dist/es/index.mjs",
|
|
6
6
|
"exports": {
|
|
@@ -37,7 +37,9 @@
|
|
|
37
37
|
"dev": "concurrently -c blue,red -n tsc,rollup --kill-others \"tsc --watch -p . --preserveWatchOutput\" \"rollup --config --watch --no-watch.clearScreen\""
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@infineit-nestjs/
|
|
40
|
+
"@infineit-nestjs/core": "1.0.5",
|
|
41
|
+
"@infineit-nestjs/types": "1.0.2",
|
|
42
|
+
"@infineit-nestjs/utils": "1.0.15",
|
|
41
43
|
"@infineit/winston-logger": "^1.0.30",
|
|
42
44
|
"@nestjs/axios": "^4.0.0",
|
|
43
45
|
"@nestjs/terminus": "^11.0.0",
|
|
@@ -93,5 +95,5 @@
|
|
|
93
95
|
"publishConfig": {
|
|
94
96
|
"access": "public"
|
|
95
97
|
},
|
|
96
|
-
"gitHead": "
|
|
98
|
+
"gitHead": "301e4212aa059ae4537c5f43c3696e9647172be3"
|
|
97
99
|
}
|