public async createQueue()

in src/queue/persistence/LokiQueueMetadataStore.ts [304:372]


  public async createQueue(
    queue: QueueModel,
    context?: Context
  ): Promise<QUEUE_STATUSCODE> {
    const coll = this.db.getCollection(this.QUEUES_COLLECTION);
    const doc = coll.findOne({
      accountName: queue.accountName,
      name: queue.name
    });

    // Check whether a conflict exists if there exist a queue with the given name.
    // If the exist queue has the same metadata as the given queue, then return 204, else throw 409 error.
    if (doc) {
      const docMeta = doc.metadata;
      const queueMeta = queue.metadata;

      // Check if both metadata is empty.
      if (queueMeta === undefined) {
        if (docMeta !== undefined) {
          throw StorageErrorFactory.getQueueAlreadyExists(
            context ? context.contextID : undefined
          );
        } else {
          return 204;
        }
      }

      if (docMeta === undefined) {
        throw StorageErrorFactory.getQueueAlreadyExists(
          context ? context.contextID : undefined
        );
      }

      // Check if the numbers of metadata are equal.
      if (Object.keys(queueMeta).length !== Object.keys(docMeta).length) {
        throw StorageErrorFactory.getQueueAlreadyExists(
          context ? context.contextID : undefined
        );
      }

      const nameMap = new Map<string, string>();
      for (const item in queueMeta) {
        if (queueMeta.hasOwnProperty(item)) {
          nameMap.set(item.toLowerCase(), item);
        }
      }

      // Check if all the metadata of exist queue is the same as another.
      for (const item in docMeta) {
        if (docMeta.hasOwnProperty(item)) {
          const queueMetaName = nameMap.get(item.toLowerCase());
          if (queueMetaName === undefined) {
            throw StorageErrorFactory.getQueueAlreadyExists(
              context ? context.contextID : undefined
            );
          }
          if (docMeta[item] !== queueMeta[queueMetaName]) {
            throw StorageErrorFactory.getQueueAlreadyExists(
              context ? context.contextID : undefined
            );
          }
        }
      }
      return 204;
    }

    coll.insert(queue);
    return 201;
  }