<commit_before>import * as express from "express";

import * as azure from "azure-storage";

import * as ulid from "ulid";

import { FiscalCode } from "../../lib/utils/fiscalcode";
import { withValidFiscalCode } from "../../lib/utils/request_validators";

import { handleErrorAndRespond } from "../../lib/utils/error_handler";

import { ICreatedMessageEvent } from "../models/created_message_event";
import { INewMessage, MessageModel } from "../models/message";

/**
 * Returns a controller that will handle requests
 * for creating new messages.
 *
 * @param Message The Message model.
 */
export function CreateMessage(
  Message: MessageModel,
  queueService: azure.QueueService,
  queueName: string,
): express.RequestHandler {
  return withValidFiscalCode((request: express.Request, response: express.Response, fiscalCode: FiscalCode) => {
    const _log: (text: any) => any = (request as any).context.log;

    const message: INewMessage = {
      bodyShort: request.body.body_short,
      fiscalCode,
      id: ulid(),
    };

    Message.createMessage(message).then((result) => {
      _log(`>> message stored [${result.id}]`);
      const createdMessage: ICreatedMessageEvent = {
        message: result,
      };
      queueService.createMessage(queueName, JSON.stringify(createdMessage), {}, (error) => {
        _log(`>> notification queued [${result.id}]`);
        // TODO: handle queue error
        response.json({
          notification: error == null,
          result,
        });
      });
    }, handleErrorAndRespond(response));

  });
}

/**
 * Returns a controller that will handle requests
 * for getting a single message for a recipient.
 *
 * @param Message The Message model<commit_msg>Fixes linting errors<commit_after>import * as express from "express";

import * as azure from "azure-storage";

import * as ulid from "ulid";

import { FiscalCode } from "../../lib/utils/fiscalcode";
import { withValidFiscalCode } from "../../lib/utils/request_validators";

import { handleErrorAndRespond } from "../../lib/utils/error_handler";

import { ICreatedMessageEvent } from "../models/created_message_event";
import { INewMessage, MessageModel } from "../models/message";

/**
 * Returns a controller that will handle requests
 * for creating new messages.
 *
 * @param Message The Message model.
 */
export function CreateMessage(
  Message: MessageModel,
  queueService: azure.QueueService,
  queueName: string,
): express.RequestHandler {
  return withValidFiscalCode((request: express.Request, response: express.Response, fiscalCode: FiscalCode) => {
    const log: (text: any) => any = (request as any).context.log;

    const message: INewMessage = {
      bodyShort: request.body.body_short,
      fiscalCode,
      id: ulid(),
    };

    Message.createMessage(message).then((result) => {
      log(`>> message stored [${result.id}]`);
      const createdMessage: ICreatedMessageEvent = {
        message: result,
      };
      queueService.createMessage(queueName, JSON.stringify(createdMessage), {}, (error) => {
        log(`>> notification queued [${result.id}]`);
        // TODO: handle queue error
        response.json({
          notification: error == null,
          result,
        });
      });
    }, handleErrorAndRespond(response));

  });
}

/**
 * Returns a controller that will handle requests
 * for getting a single message for a recipient.
 *
 * @param Message The Message model
{'fraction_non_alphanumeric': 0.13453698311007573, 'fraction_numerical': 0.0, 'mean_word_length': 3.364675984752224, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4092461', 'n_tokens_mistral': 953, 'n_tokens_neox': 952, 'n_words': 265}