update method

  1. @override
FutureOr<void> update(
  1. IndividualModel entity, {
  2. bool createOpLog = true,
})
override

The update method updates an existing entity.

Implementation

@override
FutureOr<void> update(
  IndividualModel entity, {
  bool createOpLog = true,
}) async {
  return retryLocalCallOperation(() async {
    final individualCompanion = entity.companion;

    final nameCompanion = entity.name
        ?.copyWith(
          individualClientReferenceId: entity.clientReferenceId,
          auditDetails: entity.auditDetails,
          clientAuditDetails: entity.clientAuditDetails,
        )
        .companion;

    final addressCompanions = entity.address?.map((e) {
          return e
              .copyWith(
                relatedClientReferenceId: entity.clientReferenceId,
                clientAuditDetails: entity.clientAuditDetails,
              )
              .companion;
        }).toList() ??
        [];

    final identifierCompanions = entity.identifiers?.map((e) {
          return e
              .copyWith(clientAuditDetails: entity.clientAuditDetails)
              .companion;
        }).toList() ??
        [];

    await sql.batch((batch) async {
      if (nameCompanion != null) {
        batch.update(
          sql.name,
          nameCompanion,
          where: (table) => table.individualClientReferenceId.equals(
            nameCompanion.individualClientReferenceId.value!,
          ),
        );
      }

      batch.update(
        sql.individual,
        individualCompanion,
        where: (table) => table.clientReferenceId.equals(
          entity.clientReferenceId,
        ),
      );

      batch.insertAllOnConflictUpdate(sql.address, addressCompanions);
      batch.insertAllOnConflictUpdate(sql.identifier, identifierCompanions);
    });

    await super.update(entity, createOpLog: createOpLog);
  });
}