Skip to content

Commit

Permalink
fix(notifications): implement mock data for notifications PUT, POST a…
Browse files Browse the repository at this point in the history
…ctions
  • Loading branch information
xmlking committed Dec 18, 2018
1 parent 7e0114d commit 45cfbe6
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions libs/core/src/lib/services/in-memory-data.service.ts
@@ -1,9 +1,11 @@
import {
getStatusText,
InMemoryDbService,
ParsedRequestUrl,
RequestInfo,
RequestInfoUtilities,
ResponseOptions,
STATUS,
} from 'angular-in-memory-web-api';
import { Injectable } from '@angular/core';

Expand Down Expand Up @@ -56,4 +58,49 @@ export class InMemoryDataService implements InMemoryDbService {
// console.log(`parseRequestUrl override of '${url}':`, `new: ${newUrl}`);
return parsed;
}

put(reqInfo: RequestInfo) {
const collectionName = reqInfo.collectionName;
if (collectionName === 'accounts') {
(reqInfo.req as any).body = { id: reqInfo.id, ...(reqInfo.req as any).body };
} else if (collectionName === 'notifications' || collectionName === 'subscription') {
reqInfo.collection[0].map(item => {
if (item.id === reqInfo.id) {
return { id: reqInfo.id, ...(reqInfo.req as any).body };
}
return item;
});
const options: ResponseOptions = {
status: STATUS.ACCEPTED,
};
return reqInfo.utils.createResponse$(() => {
return this.finishOptions(options, reqInfo);
});
} else {
return undefined; // let the default PUT handle all others
}
}

post(reqInfo: RequestInfo) {
const collectionName = reqInfo.collectionName;
if (collectionName === 'notifications' || collectionName === 'subscription') {
reqInfo.collection[0].push((reqInfo.req as any).body);
reqInfo.collection[1] = reqInfo.collection[1] + 1;
const options: ResponseOptions = {
status: STATUS.CREATED,
};
return reqInfo.utils.createResponse$(() => {
return this.finishOptions(options, reqInfo);
});
} else {
return undefined; // let the default POST handle all others
}
}

private finishOptions(options: ResponseOptions, { headers, url }: RequestInfo) {
options.statusText = getStatusText(options.status);
options.headers = headers;
options.url = url;
return options;
}
}

0 comments on commit 45cfbe6

Please sign in to comment.