samedi 12 juin 2021

Best way design and generate unique strings from an array of objects for mapping

The problem I am trying to solve is how best to generate a unique string from a set of question/answer strings.

So say an end user fills out a questionnaire and answers the following:

   [
       {
           "is there a problem": "yes"
       },
       {
           "what is it?": "product is damaged"
       },
       {
           "do you want a refund?": "yes"
       }
   ]

I need to generate a string that represents these questions and answers and then map that to a templateId to send a templated message to.

I thought about simply running a foreach and adding all the questions and responses together but the issue there is what if the questions and answers change or additional questions are added that we are not interested in for sending message purposes.

Should I use something like find or a hashtable type of look up instead so I am only picking out the questions I need. I am a bit worried about performance here too but it shouldn't be a big deal as this set of questions/answers should remain relatively small.

I was also wondering about this string -> templateId mapping, what is the best way to store this information, a simple object works fine?

Clarification:

It doesn't have to be unique or be a hashmap. Basically we have email templates in the backend to send messages. In the frontend we need to figure out which email template to use given the set of responses to the questions. So my simple solution was to append the questions/answers together to generate a string, i.e "isThereProblemYesWhatisIt?ProductDamagedDoyouwantaRefund?Yes"

i.e Available templates in the backend:

productDamaged
productLost

Mapping created in the front end.

"isThereProblem?YesWhatisIt?ProductDamagedDoyouwantaRefund?Yes" : 
"productDamaged"

Thanks

Aucun commentaire:

Enregistrer un commentaire