From 45cfbe698f6bad509e2926fdc4e4066499a53de7 Mon Sep 17 00:00:00 2001 From: xmlking Date: Mon, 17 Dec 2018 16:35:22 -0800 Subject: [PATCH] fix(notifications): implement mock data for notifications PUT, POST actions --- .../lib/services/in-memory-data.service.ts | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/libs/core/src/lib/services/in-memory-data.service.ts b/libs/core/src/lib/services/in-memory-data.service.ts index 8158df091..e8f1306f4 100644 --- a/libs/core/src/lib/services/in-memory-data.service.ts +++ b/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'; @@ -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; + } }