Exemple #1
0
    async def get_notifications(self, request):
        query_params: FetchNotificationsRequest = convert_request(
            FetchNotificationsRequest,
            dict(request.rel_url.query),
        )
        available_order_by_fields = {
            'created_at',
            '-created_at',
            'modified_at',
            '-modified_at',
            'scheduled_at',
            '-scheduled_at',
        }

        total, notifications = await find_notifications_by_status(
            status=query_params.status,
            start=query_params.start,
            size=query_params.size,
            order_bys=list(
                available_order_by_fields.intersection(
                    query_params.order_bys)),
        )

        return json_response(
            result={
                'total':
                total,
                'notifications': [
                    notification_model_to_dict(notification)
                    for notification in notifications
                ]
            })
Exemple #2
0
    async def create_notification(self, request):
        request: CreateNotificationRequest = convert_request(
            CreateNotificationRequest,
            await request.json(),
        )

        try:
            conditions = dataclasses.asdict(
                deserialize.deserialize(ConditionClause, request.conditions))
        except deserialize.exceptions.DeserializeException as error:
            return json_response(reason=f'wrong condition clause {error}',
                                 status=400)

        current_datetime = utc_now()

        if request.scheduled_at is None:
            scheduled_at = current_datetime
        else:
            scheduled_at = request.scheduled_at

        if current_datetime > scheduled_at:
            return json_response(
                reason=f'scheduled_at should later than current time',
                status=400)

        notification = await create_notification(
            title=request.title,
            body=request.body,
            scheduled_at=scheduled_at,
            deep_link=request.deep_link,
            image_url=request.image_url,
            icon_url=request.icon_url,
            conditions=conditions,
        )
        return json_response(result=notification_model_to_dict(notification))
Exemple #3
0
    async def search_devices(self, request):
        request: SearchDevicesRequest = convert_request(
            SearchDevicesRequest, await request.json())

        try:
            conditions: ConditionClause = deserialize.deserialize(
                ConditionClause, request.conditions)
        except deserialize.exceptions.DeserializeException as error:
            return json_response(reason=f'wrong condition clause {error}',
                                 status=400)

        total, devices = await search_devices(
            device_ids=request.device_ids,
            conditions=conditions,
            start=request.start,
            size=request.size,
            order_bys=request.order_bys,
        )

        return json_response(
            result={
                'total': total,
                'devices':
                [device_model_to_dict(device) for device in devices]
            })
Exemple #4
0
    async def search_devices(self, request):
        request: SearchDevicesRequest = convert_request(
            SearchDevicesRequest, await request.json())

        try:
            conditions: ConditionClause = deserialize.deserialize(
                ConditionClause, request.conditions)
        except deserialize.exceptions.DeserializeException as error:
            return json_response(reason=f'wrong condition clause {error}',
                                 status=400)

        total = await self.device_dispatcher.get_device_total_by_condition(
            external_ids=request.external_ids,
            condition_clause=conditions,
        )
        devices = await self.device_dispatcher.search_devices(
            external_ids=request.external_ids,
            condition_clause=conditions,
            start=request.start,
            size=request.size,
            order_bys=request.order_bys,
        )

        return json_response(
            result={
                'total': total,
                'devices': [dataclasses.asdict(device) for device in devices]
            })
Exemple #5
0
    async def increase_notification_sent_result(self, request):
        self._check_server_key(request)

        notification_uuid = request.match_info['notification_uuid']

        request_body: IncreaseNotificationSentAmountRequest = convert_request(
            IncreaseNotificationSentAmountRequest, await request.json())
        notification = await find_notification_by_id(uuid=notification_uuid)

        if notification is None:
            return json_response(
                reason=f'notification not found {notification_uuid}',
                status=404)

        if notification.status == NotificationStatus.DRAFT:
            await change_notification_status(
                target_notification=notification,
                status=NotificationStatus.SENT,
            )

        await increase_sent_count(
            uuid_=notification_uuid,
            sent_android=request_body.android,
            sent_ios=request_body.ios,
        )
        return json_response(result={
            'android': request_body.android,
            'ios': request_body.ios,
        })
Exemple #6
0
    async def create_device_notification_log(self, request):
        self._check_server_key(request)

        request_body: CreateDeviceNotificationLogRequest = convert_request(
            CreateDeviceNotificationLogRequest, await request.json())

        await add_device_notification_logs(
            device_ids=request_body.device_ids,
            notification_id=request_body.notification_id,
        )
        return json_response(result=len(request_body.device_ids))
Exemple #7
0
    async def create_device(self, request):
        request: CreateDeviceRequest = convert_request(CreateDeviceRequest,
                                                       await request.json())

        device_id = str(uuid.uuid1())
        device = await create_device(
            device_id=device_id,
            push_token=request.push_token,
            send_platform=request.send_platform,
            device_platform=request.device_platform,
        )
        return json_response(result=device_model_to_dict(row=device))
Exemple #8
0
    async def delete_properties(self, request):
        device_id = request.match_info['device_id']
        request: DeleteDevicePropertiesRequest = convert_request(
            DeleteDevicePropertiesRequest, await request.json())
        target_device = await find_device_by_device_id(device_id=device_id)

        if target_device is None:
            return json_response(reason=f'invalid device_id {device_id}',
                                 status=404)

        affected_rows = await remove_device_properties(
            target_device=target_device,
            device_properties=request.properties,
        )

        return json_response(result={'deleted': affected_rows})
Exemple #9
0
    async def update_device(self, request):
        device_id = request.match_info['device_id']
        request = convert_request(UpdateDeviceRequest, await request.json())
        target_device = await find_device_by_device_id(device_id=device_id)

        if target_device is None:
            return json_response(reason=f'invalid device_id {device_id}',
                                 status=404)

        device = await update_device(
            target_device=target_device,
            push_token=request.push_token,
            send_platform=request.send_platform,
            device_platform=request.device_platform,
        )
        return json_response(result=device_model_to_dict(row=device))
Exemple #10
0
    async def update_notification_status(self, request):
        notification_uuid = request.match_info['notification_uuid']
        notification = await find_notification_by_id(uuid=notification_uuid)

        if notification is None:
            return json_response(
                reason=f'notification not found {notification_uuid}',
                status=404)

        request_body: UpdateNotificationStatusRequest = convert_request(
            UpdateNotificationStatusRequest,
            await request.json(),
        )
        notification = await change_notification_status(
            target_notification=notification,
            status=request_body.status,
        )
        return json_response(result=notification_model_to_dict(notification))
Exemple #11
0
    async def upsert_device(self, request):
        request: UpdateDeviceRequest = convert_request(UpdateDeviceRequest,
                                                       await request.json())
        external_id = request.external_id

        device: Device = await find_device_by_external_id(
            external_id=external_id)
        if not device:
            device: Device = await create_device(external_id=external_id)

        result = await self.device_dispatcher.upsert_device_by_external_id(
            rdb_pk=device.id,
            external_id=external_id,
            push_token=request.push_token,
            send_platform=request.send_platform,
            device_platform=request.device_platform,
        )
        return json_response(result=dataclasses.asdict(result))
Exemple #12
0
    async def add_properties(self, request):
        external_id = request.match_info['external_id']
        request: AddDevicePropertiesRequest = convert_request(
            AddDevicePropertiesRequest, await request.json())
        target_device = await find_device_by_external_id(
            external_id=external_id)

        if target_device is None:
            return json_response(reason=f'invalid external_id {external_id}',
                                 status=404)

        result = None
        for property_ in request.properties:
            result = await self.device_dispatcher.upsert_property_by_external_id(
                external_id=external_id,
                property_key=property_.key,
                property_value=property_.value,
            )

        return json_response(result=dataclasses.asdict(result))
Exemple #13
0
    async def add_properties(self, request):
        device_id = request.match_info['device_id']
        request: AddDevicePropertiesRequest = convert_request(
            AddDevicePropertiesRequest, await request.json())
        target_device = await find_device_by_device_id(device_id=device_id)

        if target_device is None:
            return json_response(reason=f'invalid device_id {device_id}',
                                 status=404)

        device_properties = await add_device_properties(
            target_device=target_device,
            device_properties=request.properties,
        )

        response = device_model_to_dict(row=target_device)
        response['device_properties'] += [
            device_property_model_to_dict(device_property)
            for device_property in device_properties
        ]

        return json_response(result=response)
Exemple #14
0
    async def get_notification_events(self, request):
        device_id = request.match_info['device_id']
        query_params: FetchDeviceNotificationEventsRequest = convert_request(
            FetchDeviceNotificationEventsRequest,
            dict(request.rel_url.query),
        )
        available_order_by_fields = {
            'created_at',
            '-created_at',
            'id',
            '-id',
        }

        device = await find_device_by_device_id(device_id=device_id)

        if device is None:
            return json_response(reason=f'invalid device_id {device_id}',
                                 status=404)

        total, events = await find_notification_events_by_device_id(
            device=device,
            events=query_params.events,
            start=query_params.start,
            size=query_params.size,
            order_bys=list(
                available_order_by_fields.intersection(
                    query_params.order_bys)),
        )

        return json_response(
            result={
                'total':
                total,
                'events': [
                    device_notification_event_model_to_dict(event)
                    for event in events
                ]
            })