2021-02-16 - Translations - Functions

From Izara Wiki
Jump to navigation Jump to search

Service - Translations

Lambda Functions

Translation/Get

/**
 * Finds a matching translatesTo from an ordered list of language preferences, or return the defaultTranslatesTo translation
 * @param {string} subjectVertexTag
 * @param {string} subjectVertexUniqueId
 * @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 translationGraphTag = {from Config table: GraphTag / translation}
  3. const translatesToGraphTag = {from Config table: GraphTag / translatesTo}
  4. const defaultTranslatesToGraphTag = {from Config table: GraphTag / defaultTranslatesTo}
  5. const foundTranslation = null
  6. for each languageCodes
    1. let translatesToEdgeId = #edgeLib.createTranslatesToEdgeId
      1. subjectVertexTag: subjectVertexTag
      2. subjectVertexUniqueId: subjectVertexUniqueId
      3. languageCode: languageCode
      4. translatesToGraphTag: translatesToGraphTag
    2. let translateVertex = invoke NPM module - izara-shared#graphSharedLib.getObjectVertexByEdgeId
      1. graphServiceName: graphServiceName
      2. edgeId: translatesToEdgeId
    3. if translateVertex != null
      1. foundTranslation = translateVertex.translation
      2. break
  7. if foundTranslation == null (no translations found for array of languageCodes
    1. let defaultTranslatesToEdgeId = #edgeLib.createDefaultTranslatesToEdgeId
      1. subjectVertexTag: subjectVertexTag
      2. subjectVertexUniqueId: subjectVertexUniqueId
      3. defaultTranslatesToGraphTag: defaultTranslatesToGraphTag
    2. let translateVertex = invoke NPM module - izara-shared#graphSharedLib.getObjectVertexByEdgeId
      1. graphServiceName: graphServiceName
      2. edgeId: defaultTranslatesToEdgeId
    3. if translateVertex != null
      1. foundTranslation = translateVertex.translation
    4. else
      1. foundTranslation = (or could use a filler string - should never happen)
  8. return foundTranslation

Translation/List

/**
 * Finds all possible translations for one subject and language
 * @param {string} subjectVertexTag
 * @param {string} subjectVertexUniqueId
 * @param {string} [languageCode=""] - if empty will show all
 *
 * @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 possiblyTranslatesToGraphTag = {from Config table: GraphTag / possiblyTranslatesTo}
  3. const edgeProperties = {}
  4. if(languageCode)
    1. edgeProperties.languageCode: languageCode
  5. const translations = NPM module - izara-shared#graphSharedLib.getVertexOutEdgesAndVerticesByVertexTagAndVertexUniqueId
    1. graphServiceName: graphServiceName
    2. subjectVertexTag: subjectVertexTag
    3. subjectVertexUniqueId: subjectVertexUniqueId
    4. edgeLabel: possiblyTranslatesToGraphTag
    5. edgeProperties: edgeProperties
  6. return translations

Translation/CreateSubject

/**
 * Create a new vertex for the subject of translations
 * @param {string} vertexTag
 * @param {string} vertexUniqueId
 * @param {string} userId
 * @param {object} [vertexProperties={}]
 * @param {object} [createdByEdgeProperties={}]
 *
 */

HdrSqs

  • Triggered from MsgIn queue, msgTag: "CreateTranslationSubject"
  • Validator: (standard)

handler logic

  • (standard)

logic

  1. invoke #translationLib.createTranslationSubject
    1. vertexTag: vertexTag
    2. vertexUniqueId: vertexUniqueId
    3. userId: userId
    4. vertexProperties: vertexProperties
    5. createdByEdgeProperties: createdByEdgeProperties

Translation/Create

/**
 * Create a new translation for an existing subject vertex
 * @param {string} subjectVertexTag
 * @param {string} subjectVertexUniqueId
 * @param {string} languageCode
 * @param {string} translation
 * @param {string} userId
 * @param {object} [createdByEdgeProperties={}]
 *
 * @returns {string} translationId
 */

HdrSqs

  • Triggered from MsgIn queue, msgTag: "CreateTranslation"
  • Validator: (standard)

handler logic

  • (standard)

HdrApi

  • Validator: (standard)

Authorizer

  • AppLevel

handler logic

  • userId = ?_izContext.authorized/signedInUser..?

logic

Expects subject vertex to already exist

  1. const graphServiceName = {from Config table: TranslationsGraphServiceName}
  2. const translationGraphTag = {from Config table: GraphTag / translation}
  3. const possiblyTranslatesToGraphTag = {from Config table: GraphTag / possiblyTranslatesTo}
  4. const translatesToGraphTag = {from Config table: GraphTag / translatesTo}
  5. const defaultTranslatesToGraphTag = {from Config table: GraphTag / defaultTranslatesTo}
  6. // check any existing translations match this language and/or translation
  7. const existingTranslations = NPM module - izara-shared#graphSharedLib.getVertexOutEdgesAndVerticesByVertexTagAndVertexUniqueId
    1. graphServiceName: graphServiceName
    2. subjectVertexTag: subjectVertexTag
    3. subjectVertexUniqueId: subjectVertexUniqueId
    4. edgeLabel: possiblyTranslatesToGraphTag
  8. let hasExistingSameLanguage = false
  9. iterate existingTranslations
    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 random translationId

  1. const translationId = {random uuid}
  2. // create translation vertex
  3. publish message to graphServiceName MsgIn queue (msgTag: 'CreateChildVertexAndEdge'):
    1. subjectVertexTag: subjectVertexTag
    2. subjectVertexUniqueId: subjectVertexUniqueId
    3. vertexTag: translationGraphTag
    4. vertexUniqueId: translationId
    5. createdByUserId: userId
    6. edgeLabel: possiblyTranslatesToGraphTag
    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. // this translation set to current translatesTo with special edgeId
    2. const translatesToEdgeId = #edgeLib.createTranslatesToEdgeId
      1. subjectVertexTag: subjectVertexTag
      2. subjectVertexUniqueId: subjectVertexUniqueId
      3. languageCode: languageCode
      4. translatesToGraphTag: translatesToGraphTag
    3. publish message to graphServiceName MsgIn queue (msgTag: 'CreateEdgeFromVertexTagsAndUniqueIds'):
      1. subjectVertexTag: subjectVertexTag
      2. subjectVertexUniqueId: subjectVertexUniqueId
      3. edgeLabel: translatesToGraphTag
      4. objectVertexTag: translationGraphTag
      5. objectVertexUniqueId: translationId
      6. edgeId: translatesToEdgeId
      7. edgeProperties: {languageCode: languageCode}
  6. // report the new translation
  7. invoke #translationLib.TranslationCreatedMsgOut
    1. subjectVertexTag: subjectVertexTag
    2. subjectVertexUniqueId: subjectVertexUniqueId
    3. languageCode: languageCode
    4. translation: translation
    5. translationId: translationId
    6. userId: userId
  8. return translationId

Translation/CreateNewSubjectAndTranslation

/**
 * Create a new translation subject and an intitial translation
 * @param {string} subjectVertexTag
 * @param {string} subjectVertexUniqueId
 * @param {object} [subjectVertexProperties={}]
 * @param {string} languageCode
 * @param {string} translation
 * @param {string} userId
 * @param {object} [createdByEdgeProperties={}]
 *
 * @returns {string} translationId
 */

HdrSqs

  • Triggered from MsgIn queue, msgTag: "CreateNewSubjectAndTranslation"
  • Validator: (standard)

handler logic

  • (standard)

HdrApi

  • Validator: (standard)

Authorizer

  • AppLevel

handler logic

  • (standard)

logic

  1. const graphServiceName = {from Config table: TranslationsGraphServiceName}
  2. const translationGraphTag = {from Config table: GraphTag / translation}
  3. const possiblyTranslatesToGraphTag = {from Config table: GraphTag / possiblyTranslatesTo}
  4. const translatesToGraphTag = {from Config table: GraphTag / translatesTo}
  5. const defaultTranslatesToGraphTag = {from Config table: GraphTag / defaultTranslatesTo}
  6. // create subject vertex
  7. invoke #translationLib.createTranslationSubject
    1. vertexTag: subjectVertexTag
    2. vertexUniqueId: subjectVertexUniqueId
    3. userId: userId
    4. vertexProperties: subjectVertexProperties
    5. createdByEdgeProperties: createdByEdgeProperties

// create the random translation uniqueId

  1. const translationId = {random uuid}
  2. // create translation vertex
  3. publish message to graphServiceName MsgIn queue (msgTag: 'CreateChildVertexAndEdge'):
    1. subjectVertexTag: subjectVertexTag
    2. subjectVertexUniqueId: subjectVertexUniqueId
    3. vertexTag: translationGraphTag
    4. vertexUniqueId: translationId
    5. createdByUserId: userId
    6. edgeLabel: possiblyTranslatesToGraphTag
    7. vertexProperties: {translation: translation}
    8. edgeProperties: {languageCode: languageCode}
    9. createdByEdgeProperties: createdByEdgeProperties
  4. // this translation set to current translatesTo with special edgeId
  5. const translatesToEdgeId = #edgeLib.createTranslatesToEdgeId
    1. subjectVertexTag: subjectVertexTag
    2. subjectVertexUniqueId: subjectVertexUniqueId
    3. languageCode: languageCode
    4. translatesToGraphTag: translatesToGraphTag
  6. publish message to graphServiceName MsgIn queue (msgTag: 'CreateEdgeFromVertexTagsAndUniqueIds'):
    1. subjectVertexTag: subjectVertexTag
    2. subjectVertexUniqueId: subjectVertexUniqueId
    3. edgeLabel: translatesToGraphTag
    4. objectVertexTag: translationGraphTag
    5. objectVertexUniqueId: translationId
    6. edgeId: translatesToEdgeId
    7. edgeProperties: {languageCode: languageCode}
  7. // this translation set to defaultTranslatesTo with special edgeId
  8. const defaultTranslatesToEdgeId = #edgeLib.createDefaultTranslatesToEdgeId
    1. subjectVertexTag: subjectVertexTag
    2. subjectVertexUniqueId: subjectVertexUniqueId
    3. defaultTranslatesToGraphTag: defaultTranslatesToGraphTag
  9. publish message to graphServiceName MsgIn queue (msgTag: 'CreateEdgeFromVertexTagsAndUniqueIds'):
    1. subjectVertexTag: subjectVertexTag
    2. subjectVertexUniqueId: subjectVertexUniqueId
    3. edgeLabel: defaultTranslatesToGraphTag
    4. objectVertexTag: translationGraphTag
    5. objectVertexUniqueId: translationId
    6. edgeId: defaultTranslatesToEdgeId
    7. edgeProperties: {languageCode: languageCode}
  10. // report the new translation
  11. invoke #translationLib.TranslationCreatedMsgOut
    1. subjectVertexTag: subjectVertexTag
    2. subjectVertexUniqueId: subjectVertexUniqueId
    3. languageCode: languageCode
    4. translation: translation
    5. translationId: translationId
    6. userId: userId
  12. return translationId

Functions

edgeLib.createTranslatesToEdgeId

/**
 * @param {string} subjectVertexTag
 * @param {string} subjectVertexUniqueId
 * @param {string} languageCode
 * @param {string} translatesToGraphTag
 *
 * @returns {string} translatesToEdgeId
 */

logic

A subjectVertex can only ever have 1 translatesTo edge per language, so we can create a standardized edgeId that does not include the objectVertexId, this allows more efficient queries to find this edge and object vertex.

  1. const subjectVertexId = NPM module - izara-shared#graphSharedLib.createVertexId
    1. vertexTag = subjectVertexTag
    2. vertexUniqueId = subjectVertexUniqueId
  2. return subjectVertexId + '_' + translatesToGraphTag + '_' + languageCode

edgeLib.createDefaultTranslatesToEdgeId

/**
 * @param {string} subjectVertexTag
 * @param {string} subjectVertexUniqueId
 * @param {string} defaultTranslatesToGraphTag
 *
 * @returns {string} defaultTranslatesToEdgeId
 */

logic

A subjectVertex can only ever have 1 defaultTranslatesTo edge, so we can create a standardized edgeId that does not include the objectVertexId, this allows more efficient queries to find this edge and object vertex.

  1. return subjectVertexId + '_' + defaultTranslatesToGraphTag

translationLib.createTranslationSubject

/**
 * @param {string} vertexTag
 * @param {string} vertexUniqueId
 * @param {string} userId
 * @param {object} [vertexProperties={}]
 * @param {object} [createdByEdgeProperties={}]
 *
 */

logic

  1. const graphServiceName = {from Config table: TranslationsGraphServiceName}
  2. publish to graphServiceName MsgIn queue (msgTag: 'CreateVertex'):
    1. vertexTag: vertexTag
    2. vertexUniqueId: vertexUniqueId
    3. createdByUserId: userId
    4. vertexProperties: vertexProperties
    5. createdByEdgeProperties: createdByEdgeProperties
  3. // report this subject created
  4. send message to own MsgOut queue, msgTag: "SubjectCreated"
    1. vertexTag: vertexTag
    2. vertexUniqueId: vertexUniqueId
    3. vertexProperties: vertexProperties
    4. userId: userId

translationLib.TranslationCreatedMsgOut

/**
 * @param {string} subjectVertexTag
 * @param {string} subjectVertexUniqueId
 * @param {string} languageCode
 * @param {string} translation
 * @param {string} translationId
 * @param {string} userId
 *
 */

logic

  1. // report this translation created
  2. send message to own MsgOut queue, msgTag: "TranslationCreated"
    1. subjectVertexTag: subjectVertexTag
    2. subjectVertexUniqueId: subjectVertexUniqueId
    3. languageCode: languageCode
    4. translation: translation
    5. translationId: translationId
    6. userId: userId