upsertPhoto method

  1. @action
Future<ArticlePhoto> upsertPhoto(
  1. ArticlePhoto data, {
  2. bool isInternetAvailable = true,
})

when a photo is upsert, automatically the article is updated, this sparks a calibre sync or add the calibreId in offline queue we delete and create photo, bit more costly but much much more simple for calibrePhoto use article.id = 0

Implementation

@action
Future<ArticlePhoto> upsertPhoto(ArticlePhoto data,
    {bool isInternetAvailable = true}) async {
  // s'assurer que l'on ne crée pas des photos avec des ids identiques
  if (photos.any((e) => e.calibreId == data.calibreId && e.id == data.id)) {
    //print('within upsertPhoto ${data.toMap()}');
    // handles both sides (memory service and mobx obs photos) + server
    final isDeletedSembast =
        await _articlesService.deleteForeverPhotoRpc.request(data);
    if (isDeletedSembast) {
      final photo = await _articlesService.createPhotoRpc.request(data);
      final photoIndex = photos.indexWhere(
          (e) => e.calibreId == data.calibreId && e.id == data.id);
      photos.removeAt(photoIndex);
      photos.add(photo);
    }
  } else {
    final photo = await _articlesService.createPhotoRpc.request(data);
    photos.add(photo);
  }

  if (_gatekeeper.isLinked) {
    /// if upsert photo on cloud not possible, or if it fails
    /// then log the photo's calibre in the queue, which will resync photo when back online
    if (isInternetAvailable == false) {
      await _addCalibreInQueue(
          data.calibreId, data.dateUTC ?? DateTime.now().toUtc());
    } else {
      final statusResponse = await _upsertPhotoServer(data);
      if (statusResponse.type != StatusResponse_Type.SUCCESS) {
        await _addCalibreInQueue(
            data.calibreId, data.dateUTC ?? DateTime.now().toUtc());
      }
    }
  }
  return data;
}