getDocMeta method

  1. @override
Future<RequestResponse<DocType?>> getDocMeta({
  1. required String? doctype,
})
override

Returns the meta about a certain doctype based on the definition of Frappé's DocType.

In addition, to getting the meta, it is added to a docTypeCache as a local cache.

Returns a failed RequestResponse if the doctype doesn't exist in the backend.

Implementation

@override
Future<RequestResponse<DocType?>> getDocMeta(
    {required String? doctype}) async {
  await getFrappe().checkAppInstalled(features: ['getDocMeta']);

  if (docTypeCache.containsKey(doctype) && docTypeCache[doctype] != null) {
    if (docTypeCache.containsKey(doctype) &&
        docTypeCache[doctype] != null &&
        !docTypeCache[doctype]!.isLoading) {
      return RequestResponse.success(docTypeCache[doctype]);
    } else {
      while (docTypeCache.containsKey(doctype) &&
          docTypeCache[doctype] != null &&
          docTypeCache[doctype]!.isLoading) {
        await Future<void>.delayed(Duration(milliseconds: 100));
      }
      if (docTypeCache.containsKey(doctype) &&
          docTypeCache[doctype] != null) {
        return RequestResponse.success(docTypeCache[doctype]);
      } else {
        return RequestResponse.fail(
            handleError('get_doc_meta', ErrorDetail()));
      }
    }
  } else {
    docTypeCache[doctype] = DocType()..isLoading = true;

    // FIXME: Core-Dart doesn't support scripts currently
    //    config.coreInstance.scriptManager.events[doctype] = {};
    final response = await Request.initiateRequest(
        url: config.hostUrl,
        method: HttpMethod.POST,
        contentType: ContentTypeLiterals.APPLICATION_JSON,
        data: <String, dynamic>{
          'cmd': 'renovation_core.utils.meta.get_bundle',
          'doctype': doctype
        });

    if (response.isSuccess) {
      final metas = <String?, DocType>{};
      final metasDeserialized = List<DocType>.from(
          (response.data!.message['metas'] as List)
              .map((dynamic _meta) => DocType.fromJson(_meta))
              .toList());
      // local var first, Object.assign together later
      for (final dmeta in metasDeserialized) {
        if (dmeta.doctype == 'DocType') {
          metas[dmeta.name] = dmeta;
          // append __messages for translations

          config.coreInstance.translate.extendDictionary(
              dict: dmeta.messages != null
                  ? dmeta.messages?.cast<String, String>()
                  : null);
        }
      }
      docTypeCache.addAll(metas);
      // trigger read perm which creates the perm dict in rcore.perms.doctypePerms
      metas.forEach((String? k, DocType v) {
        if (v.isTable == null || !v.isTable!) {
          // perm for normal doctypes only, not for Child docs
          config.coreInstance.perm
              .hasPerm(doctype: k, pType: PermissionType.read);
        }
      });
      return RequestResponse.success(docTypeCache[doctype],
          rawResponse: response.rawResponse);
    }

    return RequestResponse.fail(handleError('get_doc_meta', response.error));
  }
}