def get(current_user, workspaceId, projectId): """ Get a service by service id or project id :return: Http Json response """ serviceId = request.args.get('serviceId') if serviceId: # Get by Service ID service = Service.get_by_id(serviceId) if service: return { 'serviceType': service.serviceType, 'id': service._id, 'createdBy': service.createdBy, 'createdOn': service.createdOn, 'isActive': service.isActive, 'serviceMeta': service.serviceMeta } else: return response('failed', 'service not found', 404) else: # Get by Project ID project = Project.get_by_id(projectId) services = project.services payload = [] for service_id in services: service = Service.get_by_id(service_id) payload.append({"id": service_id, "serviceType": service.serviceType, "isActive": service.isActive, "serviceMeta": service.serviceMeta}) return {'services': payload}
def deactivate(current_user, workspaceId, projectId): """ Deactivate a service Service ID Is mandatory :return: Http Json response """ if request.content_type == 'application/json': serviceId = request.get_json(force=True).get('serviceId') if serviceId: service = Service.get_by_id(serviceId) if service: service.deactivate() res_payload = { 'serviceType': service.serviceType, 'id': service._id, 'createdBy': service.createdBy, 'createdOn': service.createdOn, 'isActive': service.isActive, 'serviceMeta': service.serviceMeta } return response_with_obj('success', 'Service deactivated successfully', res_payload, 200) else: return response('failed', 'service not found', 404) else: return response('failed', 'Service ID is required in the request payload.', 402) else: return response('failed', 'Content-type must be json', 402)
def get(current_user, workspaceId, projectId): """ Get a project by project id :return: Http Json response """ project = Project.get_by_id(projectId) if project: services = project.services srv_payload = [] for sid in services: srv = Service.get_by_id(sid) if srv: srv_obj = { 'id': srv._id, 'serviceType': srv.serviceType, 'serviceMeta': srv.serviceMeta, 'isActive': srv.isActive } srv_payload.append(srv_obj) return { 'name': project.name, 'id': project._id, 'createdBy': project.createdBy, 'createdOn': project.createdOn, 'isActive': project.isActive, 'services': srv_payload, 'timezone': project.timezone } else: return response('failed', 'project not found', 404)
def publish(self, projectId, current_user, priceContract): """ Publish Bot - Triggers Billing TODO """ self.publishedOn = util.get_current_time() if not self.isPublished: self.isPublished = True ##Create a bot service serviceMeta = self.playgroundMeta serviceMeta["priceContract"] = priceContract service = Service(serviceType='bot', serviceMeta=serviceMeta, projectId=projectId, createdBy=current_user.email_id) service.create() self.publishedServiceId = service._id #add to project project = Project.get_by_id(projectId) project.services.append(service._id) project.save() self.save() else: #update service service = Service.get_by_id(self.publishedServiceId) servicePriceContract = service.serviceMeta.get( "priceContract", {}) #get exisitng price contract serviceMeta = self.playgroundMeta #copy new playground meta serviceMeta[ "priceContract"] = servicePriceContract #add priceContract to service Meta service.serviceMeta = serviceMeta #set new service meta service.save() self.save() return service._id
def manage_oerview(current_user, workspaceId, projectId): project = Project.get_by_id(projectId) playgrounds = Playground.get_by_project_id(projectId) playgroundsPayload = [] srv_to_playground = {} for playground in playgrounds: if playground.isPublished and playground.publishedServiceId: srv_to_playground[playground.publishedServiceId] = playground._id else: if "template" in playground.playgroundMeta.keys(): playground.playgroundMeta.pop("template") playgroundsPayload.append( { "id": playground._id, "createdBy": playground.createdBy, "createdOn": playground.createdOn, "lastModified": playground.lastModified, "playgroundMeta": playground.playgroundMeta, "parentMarketplaceBot": playground.parentMarketplaceBot } ) services = project.services servicesPayload = [] serviceIds = [] integrations_to_bot = {} for service_id in services: service = Service.get_by_id(service_id) if "template" in service.serviceMeta.keys(): service.serviceMeta.pop("template") if service.serviceType == "textChannel" or service.serviceType == "phone": if not integrations_to_bot.get(service.serviceMeta.get('parentBot', service_id)): integrations_to_bot[service.serviceMeta.get('parentBot', service_id)] = [] integrations_to_bot[service.serviceMeta.get('parentBot', service_id)].append({"id": service_id, "serviceType": service.serviceType, "isActive": service.isActive, "serviceMeta": service.serviceMeta}) elif service.serviceType == "bot": servicesPayload.append({"id": service_id, "serviceType": service.serviceType, "isActive": service.isActive, "playgroundId": srv_to_playground.get(service_id), "serviceMeta": service.serviceMeta}) serviceIds.append(service._id) for item in servicesPayload: item["integrations"] = integrations_to_bot.get(item['id'], []) combinedPayload = { "playgrounds": playgroundsPayload, "services": servicesPayload } return make_response(jsonify({ 'status': "success", 'message': "Payload retrieved successfully", 'service': combinedPayload })), 200