UNPKG

channelape-sdk

Version:
71 lines 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Q = require("q"); const Version_1 = require("../../model/Version"); const Resource_1 = require("../../model/Resource"); const GenerateApiError_1 = require("../../utils/GenerateApiError"); const EXPECTED_GET_STATUS = 200; const EXPECTED_PUT_STATUS = 200; const EXPECTED_POST_STATUS = 201; class PlaysService { constructor(client, stepsService) { this.client = client; this.stepsService = stepsService; } get(playId) { const deferred = Q.defer(); const requestUrl = playId ? `/${Version_1.default.V2}${Resource_1.default.PLAYS}/${playId}` : `/${Version_1.default.V2}${Resource_1.default.PLAYS}`; this.client.get(requestUrl, {}, (error, response, body) => { this.mapPlayPromise(requestUrl, deferred, error, response, body, EXPECTED_GET_STATUS); }); return deferred.promise; } create(play) { const deferred = Q.defer(); const requestUrl = `/${Version_1.default.V2}${Resource_1.default.PLAYS}`; this.client.post(requestUrl, { data: play }, (error, response, body) => { this.mapPlayPromise(requestUrl, deferred, error, response, body, EXPECTED_POST_STATUS); }); return deferred.promise; } update(play) { const deferred = Q.defer(); const requestUrl = `/${Version_1.default.V2}${Resource_1.default.PLAYS}/${play.id}`; this.client.put(requestUrl, { data: play }, (error, response, body) => { this.mapPlayPromise(requestUrl, deferred, error, response, body, EXPECTED_PUT_STATUS); }); return deferred.promise; } mapPlayPromise(requestUrl, deferred, error, response, body, expectedStatusCode) { if (error) { deferred.reject(error); } else if (response.status === expectedStatusCode) { const plays = body.plays ? body.plays.map((play) => this.formatPlay(play)) : this.formatPlay(body); deferred.resolve(plays); } else { const playApeErrorResponse = (0, GenerateApiError_1.default)(requestUrl, response, body, EXPECTED_GET_STATUS); deferred.reject(playApeErrorResponse); } } formatPlay(play) { play.createdAt = new Date(play.createdAt); play.updatedAt = new Date(play.updatedAt); if (play.steps) { play.steps = play.steps.map((item) => this.stepsService.formatStep(item)); } if (play.scheduleConfigurations) { play.scheduleConfigurations = play.scheduleConfigurations.map((item) => this.formatPlaySchedule(item)); } return play; } formatPlaySchedule(playScheduleConfiguration) { playScheduleConfiguration.createdAt = new Date(playScheduleConfiguration.createdAt); playScheduleConfiguration.updatedAt = new Date(playScheduleConfiguration.updatedAt); return playScheduleConfiguration; } } exports.default = PlaysService; //# sourceMappingURL=PlaysService.js.map