Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@ This is a basic repo template for building new MapColonies web services in Types
## API
Checkout the OpenAPI spec [here](/openapi3.yaml)

userData Service: <br/>
We use 3rd party software in order to extract user details from the userId. Here is a mock service that will preduce the somewhat expected response from the userData Service.
```
const express = require('express');

const app = express();
app.set('port', 5000);

app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));

app.get("/mirage/:userid", (req, res) => {
console.log("new request", req.params, req.query);
const { userid } = req.params;
const users = {
"avi@mapcolonies.net": {
firstName: "avi",
lastName: "map",
displayName: "mapcolonies/avi",
mail: "avi@mapcolonies.net",
domains: ["USA", "FRANCE"],
},
};
const user = users[userid] ? users[userid] : { [userid]: null };
return res.status(200).json(user);
});

app.listen(app.get('port'), () => {
console.log(`Server is running on port ${app.get('port')}`);
});
```

## Installation

Install deps with npm
Expand Down
4 changes: 4 additions & 0 deletions config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
"application": {
"userDataService": {
"endpoint": "USER_DATA_SERVICE_ENDPOINT",
"headers": {
"__name": "HEADER_PARAMS",
"__format": "json"
},
"queryParams": {
"__name": "USER_DATA_SERVICE_QUERY_PARAMS",
"__format": "json"
Expand Down
3 changes: 3 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
"endpoint": "http://user-data-service:8080",
"queryParams": {
"extraDetails": true
},
"headers": {
"headerDetails": true
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/common/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export interface QueryResult extends FeatureCollection {
export interface IApplication {
userDataService: {
endpoint: string;
headers?: {
[key: string]: string | number | boolean;
};
queryParams?: {
[key: string]: string | number | boolean;
};
Expand Down
5 changes: 3 additions & 2 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ const axiosInstance = axios.create({
export const fetchUserDataService = async (
endpoint: string,
userId: string,
queryParams?: { [key: string]: string | number | boolean | string[] }
queryParams?: { [key: string]: string | number | boolean | string[] },
headerParams?: { [key: string]: string | number | boolean | string[] }
): Promise<UserDataServiceResponse> => {
let res: AxiosResponse | null = null;

const stringQueryParams = Object.entries(queryParams ?? {})
.map(([key, value]) => `${key}=${String(value)}`)
.join('&');

res = await axiosInstance.get(`${endpoint}/${userId}?${stringQueryParams}`);
res = await axiosInstance.get(`${endpoint}/${userId}?${stringQueryParams}`, { headers: headerParams });

return (res?.data ?? {}) as UserDataServiceResponse;
};
4 changes: 2 additions & 2 deletions src/process/models/processManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class ProcessManager {
score = selectedResponse.properties._score;
}

const { endpoint, queryParams } = this.appConfig.userDataService;
const fetchedUserData = await fetchUserDataService(endpoint, feedbackResponse.geocodingResponse.userId, queryParams);
const { endpoint, queryParams, headers } = this.appConfig.userDataService;
const fetchedUserData = await fetchUserDataService(endpoint, feedbackResponse.geocodingResponse.userId, queryParams, headers);

return {
user: {
Expand Down
6 changes: 5 additions & 1 deletion tests/integration/process/process.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ describe('process', function () {
describe('Happy Path', function () {
it('should return 200 status code and the resource', async function () {
const userId = 'avi@mapcolonies.net';
const userDataServiceScope = nock(config.get<IApplication>('application').userDataService.endpoint)
const userDataServiceScope = nock(config.get<IApplication>('application').userDataService.endpoint, {
reqheaders: {
headerDetails: () => true,
},
})
.get(`/${userId}?extraDetails=true`)
.reply(httpStatusCodes.OK, {
firstName: 'avi',
Expand Down