2021-02-16 - Translations - Functions

From Izara Wiki
Jump to navigation Jump to search

Service - Translations

Lambda Functions

Translation/Get

/**
 * 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#graphSharedLib.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#graphSharedLib.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

Translation/ListOneLanguage

/**
 * 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#graphSharedLib.getVertexOutEdgesAndVertices
    1. graphServiceName: graphServiceName
    2. subjectVertexId: subjectVertexId
    3. edgeLabel: NPM module - izara-market-shared#TranslationsLib.possiblyTranslatesToGraphTag
    4. edgeProperties: {languageCode: languageCode}
  3. return translations

Translation/CreateSubject

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

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. vertexTag: vertexTag
    2. vertexUniqueId: vertexUniqueId
    3. createdByUserId: ?_izContext.authorized/signedInUser..?
    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} 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}
  1. // check existing translations match this language and/or translation
  2. const existingTranslations = NPM module - izara-shared#graphSharedLib.getVertexOutEdgesAndVertices
    1. graphServiceName: graphServiceName
    2. subjectId: subjectVertexId
    3. edgeLabel: NPM module - izara-market-shared#TranslationsLib.possiblyTranslatesToGraphTag
  3. let hasExistingSameLanguage = false
  4. 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 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: NPM module - izara-market-shared#TranslationsLib.translationGraphTag
    3. uniqueId: translationId
    4. createdByUserId: createdByUserId
    5. edgeLabel: NPM module - izara-market-shared#TranslationsLib.possiblyTranslatesToGraphTag
    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: NPM module - izara-market-shared#TranslationsLib.translatesToGraphTag
      3. edgeId: translatesToEdgeId
      4. objectId: translationVertexId
      5. edgeProperties:
        1. languageCode: languageCode

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} createdByUserId
 * @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. publish to graphServiceName MsgIn queue (msgTag: 'CreateVertex'):
    1. vertexTag: subjectVertexTag
    2. vertexUniqueId: subjectVertexUniqueId
    3. createdByUserId: createdByUserId
    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: createdByUserId
    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 = #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 = #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. send message to own MsgOut queue, msgTag: "TranslationCreated"
    1. subjectVertexTag: subjectVertexTag
    2. subjectVertexUniqueId: subjectVertexUniqueId
    3. translationId: translationId
    4. languageCode: languageCode
    5. translation: translation
    6. createdByUserId: createdByUserId
  12. return translationId

Functions

createTranslationVertexId

                      • probably delete this function
/**
 * @param {string} translationId
 *
 * @returns {string} translatesToEdgeId or null if none found
 */

logic

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

    1. vertexTag: NPM module - izara-market-shared#TranslationsLib.translationGraphTag
    2. uniqueId: translationId

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

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 + '_' + translationLabel