2021-02-16 - Translations - Functions

From Izara Wiki
Jump to navigation Jump to search

Service - Translations

Lambda Functions

GetTranslation

/**
 * Finds a matching translation from an ordered list of language preferences, or return the defaultTranslatesTo translation
 * @param {string} subjectVertexId
 * @param {string} translationLabel
 * @param {string[]} languageCodes - is an array of languageCodes, ordered by preference
 *
 * @returns {string} translation
 */

HdrApi

Validator: (standard)

handler logic

(standard)

logic

  1. const graphServiceName = {from Config table: TranslationsGraphServiceName}
  2. const foundTranslation = null
  3. for each languageCodes
    1. let translatesToEdgeId = #createTranslatesToEdgeId
      1. subjectVertexId
      2. translationLabel
      3. languageCode
    2. let translateVertex = invoke NPM module - izara-shared#neptuneGraphSharedLib.getObjectVertexByEdgeId
      1. edgeId: translatesToEdgeId
    3. if translateVertex != null
      1. foundTranslation = translateVertex.translation
      2. break
  4. if foundTranslation == null (no translations found for array of languageCodes
    1. let defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
      1. subjectVertexId
      2. translationLabel
    2. let translateVertex = invoke NPM module - izara-shared#neptuneGraphSharedLib.getObjectVertexByEdgeId
      1. edgeId: defaultTranslatesToEdgeId
    3. if translateVertex != null
      1. foundTranslation = translateVertex.translation
    4. else
      1. foundTranslation = (or could use a filler string - should never happen)
  5. return foundTranslation

ListTranslationsOneLanguage

/**
 * Finds all possible translations for one subject and language
 * @param {string} subjectVertexId
 * @param {string[]} languageCode
 *
 * @returns {object[]} array of translation objects (maybe edges as well)
 */

HdrApi

Validator: (standard)

handler logic

(standard)

logic

  1. const graphServiceName = {from Config table: TranslationsGraphServiceName}
  2. const translations = NPM module - izara-shared#neptuneGraphSharedLib.getVertexOutEdgesAndVertices
    1. graphServiceName: graphServiceName
    2. subjectVertexId: subjectVertexId
    3. edgeLabel: 'possiblyTranslatesTo'
    4. edgeProperties: {languageCode: languageCode}
  3. return translations

CreateSubjectVertex

/**
 * Create a new vertex for the subject of translations
 * @param {string} vertexId
 * @param {string} vertexLabel
 * @param {object} [vertexProperties]
 *
 */

HdrSqs

Triggered from MsgIn queue, msgTag: "CreateSubjectVertex"

Validator: (standard)

handler logic

(standard)

logic

  1. const graphServiceName = {from Config table: TranslationsGraphServiceName}
  2. publish to graphServiceName MsgIn queue (msgTag: 'CreateVertex'):
    1. vertexId: vertexId
    2. vertexLabel: vertexLabel
    3. vertexProperties: vertexProperties

CreateTranslation

/**
 * Create a new translation for a source object
 * @param {string} subjectVertexId
 * @param {string} translationLabel
 * @param {string} languageCode
 * @param {string} translation
 * @param {string} createdByUserId
 * @param {object} [createdByEdgeProperties={}]
 *
 * @returns {string} translationVertexId
 */

HdrSqs

Triggered from MsgIn queue, msgTag: "CreateTranslation"

Validator: (standard)

handler logic

(standard)

HdrApi

Validator: (standard)

Authorizer

AppLevel

handler logic

(standard)

logic

Expects subject vertex to already exist

  1. const graphServiceName = {from Config table: TranslationsGraphServiceName}
  2. // check existing translations match this language and/or translation
  3. const existingVertices = NPM module - izara-shared#neptuneGraphSharedLib.getVertexOutEdgesAndVertices
    1. graphServiceName: graphServiceName
    2. subjectId: subjectVertexId
    3. edgeLabel: 'possiblyTranslatesTo'
  4. let hasExistingSameLanguage = false
  5. iterate existingVertices
    1. if any match this languageCode and translation can return, no additional work to be done
    2. if any match this languageCode set hasExistingSameLanguage = true

// create this translations vertex

  1. const translationId = {random uuid}

// create translationVertexId for additional edges if needed, also gets created separately in CreateChildVertexAndEdge Lambda

  1. const translationVertexId = #createTranslationVertexId
    1. translationId: translationId
  2. // no existing translations match this language and translation, so create translation vertex
  3. publish message to graphServiceName MsgIn queue (msgTag: 'CreateChildVertexAndEdge'):
    1. subjectVertexId: subjectVertexId
    2. vertexLabel: 'translation'
    3. uniqueId: translationId
    4. createdByUserId: createdByUserId
    5. edgeLabel: 'possiblyTranslatesTo'
    6. edgeUniqueSubId: translationLabel + '_' + languageCode
    7. vertexProperties: {translation: translation}
    8. edgeProperties: {languageCode: languageCode}
    9. createdByEdgeProperties: createdByEdgeProperties
  4. // if there are no other translations for this language, this translation becomes the current translatesTo
  5. if hasExistingSameLanguage == false
    1. const translatesToEdgeId = #createTranslatesToEdgeId
      1. subjectVertexId
      2. translationLabel
      3. languageCode
    2. create translatesTo edge by publish message to Service - Graph Handler MsgIn queue (msgTag: 'CreateEdge'):
      1. subjectId: subjectVertexId
      2. edgeLabel: 'translatesTo'
      3. edgeId: translatesToEdgeId
      4. objectId: translationVertexId
      5. edgeProperties:
        1. languageCode: languageCode

CreateNewTranslationSubjectAndTranslation

/**
 * Create a new translation subject and an intitial translation
 * @param {string} subjectVertexId
 * @param {string} subjectVertexLabel
 * @param {object} [subjectVertexProperties={}]
 * @param {string} translationLabel
 * @param {string} languageCode
 * @param {string} translation
 * @param {string} createdByUserId
 * @param {object} [createdByEdgeProperties={}]
 *
 * @returns {string} translationVertexId
 */

HdrSqs

Triggered from MsgIn queue, msgTag: "CreateTranslation"

Validator: (standard)

handler logic

(standard)

HdrApi

Validator: (standard)

Authorizer

AppLevel

handler logic

(standard)

logic

  1. const graphServiceName = {from Config table: TranslationsGraphServiceName}
  2. publish to graphServiceName MsgIn queue (msgTag: 'CreateVertex'):
    1. vertexId: subjectVertexId
    2. vertexLabel: subjectVertexLabel
    3. vertexProperties: subjectVertexProperties

// create this translations vertex

  1. const translationId = {random uuid}

// create translationVertexId, also gets created separately in CreateChildVertexAndEdge Lambda

  1. const translationVertexId = #createTranslationVertexId
    1. translationId: translationId
  2. // create translation vertex
  3. publish message to graphServiceName MsgIn queue (msgTag: 'CreateChildVertexAndEdge'):
    1. subjectVertexId: subjectVertexId
    2. vertexLabel: 'translation'
    3. uniqueId: translationId
    4. createdByUserId: createdByUserId
    5. edgeLabel: 'possiblyTranslatesTo'
    6. edgeUniqueSubId: translationLabel + '_' + languageCode
    7. vertexProperties: {translation: translation}
    8. edgeProperties: {languageCode: languageCode}
    9. createdByEdgeProperties: createdByEdgeProperties
  4. // this translation set to current translatesTo
  5. const translatesToEdgeId = #createTranslatesToEdgeId
    1. subjectVertexId
    2. translationLabel
    3. languageCode
  6. create translatesTo edge by publish message to Service - Graph Handler MsgIn queue (msgTag: 'CreateEdge'):
    1. subjectId: subjectVertexId
    2. edgeLabel: 'translatesTo'
    3. edgeId: translatesToEdgeId
    4. objectId: translationVertexId
    5. edgeProperties:
      1. languageCode: languageCode
  7. // this translation set to defaultTranslatesTo
  8. const defaultTranslatesToEdgeId = #createDefaultTranslatesToEdgeId
    1. subjectVertexId
    2. translationLabel
  9. create defaultTranslatesTo edge by publish message to Service - Graph Handler MsgIn queue (msgTag: 'CreateEdge'):
    1. subjectId: subjectVertexId
    2. edgeLabel: 'defaultTranslatesTo'
    3. edgeId: defaultTranslatesToEdgeId
    4. objectId: translationVertexId

Functions

createTranslationVertexId

/**
 * @param {string} translationId
 *
 * @returns {string} translatesToEdgeId or null if none found
 */

logic

return = NPM module - izara-shared#neptuneGraphSharedLib.createVertexId

    1. vertexTag: 'translation'
    2. uniqueId: translationId

createTranslatesToEdgeId

/**
 * @param {string} subjectVertexId
 * @param {string} translationLabel
 * @param {string} languageCode
 *
 * @returns {string} translatesToEdgeId or null if none found
 */

logic

  1. return subjectVertexId + '_' + translationLabel + '_' + languageCode

createDefaultTranslatesToEdgeId

/**
 * @param {string} subjectVertexId
 * @param {string} translationLabel
 *
 * @returns {string} defaultTranslatesToEdgeId or null if none found
 */

logic

  1. return subjectVertexId + '_Default_' + translationLabel