public async create()

in source/packages/services/events-processor/src/api/targets/target.service.ts [120:216]


    public async create(
        item: TargetItem,
        topicArn?: string,
        principalValue?: string,
        event?: EventItem,
        skipDao?: boolean
    ): Promise<CreateTargetResponse> {
        logger.debug(
            `target.service create: in: item:${JSON.stringify(
                item
            )}, topicArn:${topicArn}, principalValue:${principalValue}, event:${JSON.stringify(
                event
            )}, skipDao:${skipDao}`
        );

        // validate input
        ow(item, ow.object.nonEmpty);
        ow(item.subscriptionId, ow.string.nonEmpty);
        ow(item.targetType, ow.string.nonEmpty);

        // retrieve the topicArn if not provided
        let subscription;
        if (topicArn === undefined) {
            subscription = await this.getSubscription(item.subscriptionId);
            topicArn = subscription.sns?.topicArn;
        }

        // retrieve the principalValue if not provided
        if (principalValue === undefined) {
            if (subscription === undefined) {
                subscription = await this.getSubscription(item.subscriptionId);
            }
            principalValue = subscription.principalValue;
        }

        // retrieve the event if not provided
        if (event === undefined) {
            if (subscription === undefined) {
                subscription = await this.getSubscription(item.subscriptionId);
            }
            event = await this.getEvent(subscription.event.id);
        }

        // verify target is allowed. if so, create it.
        switch (item.targetType) {
            case 'email':
                ow(event.supportedTargets.email, ow.string.nonEmpty);
                await this.emailTarget.create(item as EmailTargetItem, topicArn);
                break;
            case 'sms':
                ow(event.supportedTargets.sms, ow.string.nonEmpty);
                await this.smsTarget.create(item as SMSTargetItem, topicArn);
                break;
            case 'mqtt':
                ow(event.supportedTargets.mqtt, ow.string.nonEmpty);
                // TODO:
                break;
            case 'dynamodb':
                ow(event.supportedTargets.dynamodb, ow.string.nonEmpty);
                await this.dynamodbTarget.ensureTableExists(
                    (item as DynamodDBTargetItem).tableName
                );
                break;
            case 'push_gcm':
                ow(event.supportedTargets.push_gcm, ow.string.nonEmpty);
                await this.pushTarget.create(item as PushTargetItem, topicArn);
                break;
            case 'push_adm':
                ow(event.supportedTargets.push_adm, ow.string.nonEmpty);
                await this.pushTarget.create(item as PushTargetItem, topicArn);
                break;
            case 'push_apns':
                ow(event.supportedTargets.push_apns, ow.string.nonEmpty);
                await this.pushTarget.create(item as PushTargetItem, topicArn);
                break;
            default:
                throw new Error('UNSUPPORTED_TARGET_TYPE');
        }

        // save the target info (may be skipped if called from a subscription dao for efficieny)
        if (!skipDao) {
            await this.targetDao.create(
                item,
                event.eventSourceId,
                event.principal,
                principalValue
            );
        }

        const res: CreateTargetResponse = {
            subscriptionId: item.subscriptionId,
            targetType: item.targetType,
            targetId: item.getId(),
        };
        logger.debug(`targetDao.service create: exit:${JSON.stringify(res)}`);
        return res;
    }