public toResource()

in source/packages/services/events-processor/src/api/subscriptions/subscription.assembler.ts [127:197]


    public toResource(item: SubscriptionItem, version: string): SubscriptionBaseResource {
        logger.debug(
            `subscription.assembler toResource: in: item:${JSON.stringify(
                item
            )}, version:${version}`
        );

        let resource: SubscriptionBaseResource;
        if (version.startsWith('1.')) {
            // v1 specific...
            if (
                item.targets?.dynamodb?.length > 1 ||
                item.targets?.email?.length > 1 ||
                item.targets?.mqtt?.length > 1 ||
                item.targets?.push_gcm?.length > 1 ||
                item.targets?.sms?.length > 1 ||
                item.targets?.push_adm?.length > 1 ||
                item.targets?.push_apns?.length > 1
            ) {
                throw new Error('INVALID_RESOURCE_VERSION');
            }
            resource = new SubscriptionV1Resource();
            const asV1 = resource as SubscriptionV1Resource;
            asV1.targets = {
                dynamodb: this.targetAssembler.toResource(item.targets?.dynamodb?.[0]),
                email: this.targetAssembler.toResource(item.targets?.email?.[0]),
                mqtt: this.targetAssembler.toResource(item.targets?.mqtt?.[0]),
                push_gcm: this.targetAssembler.toResource(item.targets?.push_gcm?.[0]),
                push_adm: this.targetAssembler.toResource(item.targets?.push_adm?.[0]),
                push_apns: this.targetAssembler.toResource(item.targets?.push_apns?.[0]),
                sms: this.targetAssembler.toResource(item.targets?.sms?.[0]),
            };

            if (asV1.targets?.email) {
                if (this.snsTarget.isPendingConfirmation(asV1.targets.email.subscriptionArn)) {
                    asV1.targets.email.subscriptionArn = 'Pending confirmation';
                }
            }
        } else {
            // v2 specific...
            resource = new SubscriptionV2Resource();
            const asV2 = resource as SubscriptionV2Resource;
            asV2.targets = {
                dynamodb: this.targetAssembler.toResources(item.targets?.dynamodb),
                email: this.targetAssembler.toResources(item.targets?.email),
                mqtt: this.targetAssembler.toResources(item.targets?.mqtt),
                push_gcm: this.targetAssembler.toResources(item.targets?.push_gcm),
                push_adm: this.targetAssembler.toResources(item.targets?.push_adm),
                push_apns: this.targetAssembler.toResources(item.targets?.push_apns),
                sms: this.targetAssembler.toResources(item.targets?.sms),
            };

            if (asV2.targets?.email) {
                asV2.targets.email.forEach((t) => {
                    if (this.snsTarget.isPendingConfirmation(t.subscriptionArn)) {
                        t.subscriptionArn = 'Pending confirmation';
                    }
                });
            }
        }

        // common properties
        Object.keys(item).forEach((key) => {
            if (key !== 'targets') {
                resource[key] = item[key];
            }
        });

        logger.debug(`subscription.assembler toResource: exit: node: ${JSON.stringify(resource)}`);
        return resource;
    }