<commit_before>'use strict'

const fbMessenger = require('./lib/fb-messenger')
const firstEntityValue = require('./utils').firstEntityValue

const movie = require('./lib/movie')
const cinema = require('./lib/cinema')
const templates = require('./templates')
const sessions = require('./sessions')

const actions = {
  say (sessionId, context, message, cb) {
    fbMessenger.send(sessions.list[sessionId].fbid, message, cb)
  },
  merge (sessionId, context, entities, message, cb) {
    // console.log(context, entities)
    const searchMovieTitle = firstEntityValue(entities, 'movie')
    if (searchMovieTitle) context.searchMovieTitle = searchMovieTitle

    const searchPostcode = firstEntityValue(entities, 'location')
    if (searchPostcode) context.searchPostcode = searchPostcode

    const searchDatetime = firstEntityValue(entities, 'datetime')
    if (searchDatetime) context.searchDatetime = searchDatetime

    cb(context)
  },
  error (sessionId, context, error) {
    fbMessenger.send(error.message)
  },
  findCinemasByMovie (sessionId, context, cb) {
    sessions.list[sessionId].lastAction = 'findCinemasByMovie'

    movie
      .findByName(context.searchMovieTitle)
      .then((movie) => {
        const d = new Date(context.searchDatetime)
        const year = d.getFullYear()
        let month = d.getMonth() + 1
        let day = d.getDate()

        month = month > 9 ? month : `0${month}`
        day = day > 9 ? day : `0${day}`

        const postcode = context.searchPostcode.replace(/\s/g, '')

        return cinema
          .findByMovieDatePostcode(movie.film_id, `${year}-${month}-${day}`, postcode)
      })
      .then((cinema) => {
        context.resultText = templates.byMovie(context, cinema)
        sessions.list[sessionId]
        cb(context)
      })
      .catch((err) => {
        context.resultText = `${err.message}.`
        cb(context)
      })
  },
  findCinemasByLocation (sessionId, context, cb) {
    sessions.list[sessionId].lastAction = 'findCinemasByLocation'

    const postcode = context.searchPostcode.replace(/\s/g, '')
    cinema
      .findByPostcode(postcode)
      .then((data) => {
        context.resultText = templates.byLocation(context, data)
        cb(context)
      })
      .catch((err) => {
        context.resultText = `${err.message}.`
        cb(context)
      })
  }<commit_msg>fix multiple context reference (4)<commit_after>'use strict'

const fbMessenger = require('./lib/fb-messenger')
const firstEntityValue = require('./utils').firstEntityValue

const movie = require('./lib/movie')
const cinema = require('./lib/cinema')
const templates = require('./templates')
const sessions = require('./sessions')

const actions = {
  say (sessionId, context, message, cb) {
    fbMessenger.send(sessions.list[sessionId].fbid, message, cb)
  },
  merge (sessionId, context, entities, message, cb) {
    // console.log(context, entities)
    const searchMovieTitle = firstEntityValue(entities, 'movie')
    if (searchMovieTitle) context.searchMovieTitle = searchMovieTitle

    const searchPostcode = firstEntityValue(entities, 'location')
    if (searchPostcode) context.searchPostcode = searchPostcode

    const searchDatetime = firstEntityValue(entities, 'datetime')
    if (searchDatetime) context.searchDatetime = searchDatetime

    cb(context)
  },
  error (sessionId, context, error) {
    fbMessenger.send(error.message)
  },
  findCinemasByMovie (sessionId, context, cb) {
    // sessions.list[sessionId].lastAction = 'findCinemasByMovie'
    context.lastAction = 'findCinemasByMovie'

    movie
      .findByName(context.searchMovieTitle)
      .then((movie) => {
        const d = new Date(context.searchDatetime)
        const year = d.getFullYear()
        let month = d.getMonth() + 1
        let day = d.getDate()

        month = month > 9 ? month : `0${month}`
        day = day > 9 ? day : `0${day}`

        const postcode = context.searchPostcode.replace(/\s/g, '')

        return cinema
          .findByMovieDatePostcode(movie.film_id, `${year}-${month}-${day}`, postcode)
      })
      .then((cinema) => {
        context.resultText = templates.byMovie(context, cinema)
        cb(context)
      })
      .catch((err) => {
        context.resultText = `${err.message}.`
        cb(context)
      })
  },
  findCinemasByLocation (sessionId, context, cb) {
    // sessions.list[sessionId].lastAction = 'findCinemasByLocation'
    context.lastAction = 'findCinemasByLocation'

    const postcode = context.searchPostcode.replace(/\s/g, '')
    cinema
      .findByPostcode(postcode)
      .then((data) => {
        context.resultText = templates.byLocation(context, data)
        cb(context)
      })
      .catch((err) => {
        context.resultText = `${err.message}.`
        cb(context)
      })
  }
{'fraction_non_alphanumeric': 0.1377944548676256, 'fraction_numerical': 0.002293099854075464, 'mean_word_length': 3.0834042553191487, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '5473370', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1446, 'n_words': 285}