public async saveDeviceTask()

in source/packages/services/greengrass2-provisioning/src/deviceTasks/deviceTasks.dao.ts [109:249]


    public async saveDeviceTask(task: DeviceTaskItem, saveBatchProgress: boolean): Promise<void> {
        logger.debug(
            `coreTasks.dao saveCoreTask: in: task:${JSON.stringify(
                task
            )}, saveBatchProgress:${saveBatchProgress}`
        );

        ow(task, ow.object.nonEmpty);
        ow(task.id, ow.string.nonEmpty);
        ow(task.taskStatus, ow.string.nonEmpty);

        if ((task.devices?.length ?? 0) > 0) {
            for (const device of task.devices) {
                ow(device?.name, ow.string.nonEmpty);
            }
        }

        const params: BatchWriteCommandInput = {
            RequestItems: {
                [process.env.AWS_DYNAMODB_TABLE_NAME]: [],
            },
        };

        // main task item
        const taskDbId = createDelimitedAttribute(PkType.ClientDeviceTask, task.id);
        const taskItem = {
            PutRequest: {
                Item: {
                    pk: taskDbId,
                    sk: taskDbId,
                    siKey1: PkType.ClientDeviceTask,
                    taskId: task.id,
                    taskStatus: task.taskStatus,
                    statusMessage: task.statusMessage,
                    type: task.type,
                    options: task.options,
                    coreName: task.coreName,
                    createdAt: task.createdAt?.toISOString(),
                    updatedAt: task.updatedAt?.toISOString(),
                },
            },
        };
        params.RequestItems[process.env.AWS_DYNAMODB_TABLE_NAME].push(taskItem);

        // batch processing status item
        if (task.batchesTotal > 0 && saveBatchProgress) {
            const batchDbId = createDelimitedAttribute(
                PkType.ClientDeviceTask,
                task.id,
                'batches'
            );
            const batchSummaryItem = {
                PutRequest: {
                    Item: {
                        pk: taskDbId,
                        sk: batchDbId,
                        batchesTotal: task.batchesTotal,
                        batchesComplete: task.batchesComplete,
                        createdAt: task.createdAt?.toISOString(),
                        updatedAt: task.updatedAt?.toISOString(),
                    },
                },
            };
            params.RequestItems[process.env.AWS_DYNAMODB_TABLE_NAME].push(batchSummaryItem);
        }

        if ((task.devices?.length ?? 0) > 0) {
            for (const device of task.devices) {
                // client device task detail item
                const deviceDbId = createDelimitedAttribute(PkType.ClientDevice, device.name);
                const clientDeviceTaskDetailItem = {
                    PutRequest: {
                        Item: {
                            pk: taskDbId,
                            sk: deviceDbId,
                            name: device.name,
                            coreName: device.coreName,
                            taskStatus: (device as DeviceItem).taskStatus,
                            statusMessage: (device as DeviceItem).statusMessage,
                            createdAt: (device as DeviceItem).createdAt?.toISOString(),
                            updatedAt: (device as DeviceItem).updatedAt?.toISOString(),
                        },
                    },
                };
                params.RequestItems[process.env.AWS_DYNAMODB_TABLE_NAME].push(
                    clientDeviceTaskDetailItem
                );
                // client device thing item
                const clientDeviceItem = {
                    PutRequest: {
                        Item: {
                            pk: deviceDbId,
                            sk: deviceDbId,
                            name: device.name,
                            siKey1: PkType.ClientDevice,
                            siKey2: createDelimitedAttribute(PkType.CoreDevice, device.coreName),
                            siSort2: deviceDbId,
                            createdAt: (device as DeviceItem).createdAt?.toISOString(),
                            updatedAt: (device as DeviceItem).updatedAt?.toISOString(),
                        },
                    },
                };
                params.RequestItems[process.env.AWS_DYNAMODB_TABLE_NAME].push(clientDeviceItem);

                if ((device as DeviceItem).artifacts !== undefined) {
                    for (const [name, artifact] of Object.entries(
                        (device as DeviceItem).artifacts
                    )) {
                        const artifactDbId = createDelimitedAttribute(
                            PkType.ClientDevice,
                            device.name,
                            PkType.Artifact,
                            name
                        );
                        const artifactItem = {
                            PutRequest: {
                                Item: {
                                    pk: deviceDbId,
                                    sk: artifactDbId,
                                    name,
                                    bucket: artifact.bucket,
                                    key: artifact.key,
                                    createdAt: artifact.createdAt?.toISOString(),
                                },
                            },
                        };
                        params.RequestItems[process.env.AWS_DYNAMODB_TABLE_NAME].push(
                            artifactItem
                        );
                    }
                }
            }
        }

        const result = await this.dynamoDbUtils.batchWriteAll(params);
        if (this.dynamoDbUtils.hasUnprocessedItems(result)) {
            throw new Error('SAVE_DEVICE_TASK_FAILED');
        }

        logger.debug(`deviceTasks.dao saveDeviceTask: exit:`);
    }