<commit_before>import {
  GeneralRequestData,
} from '@phenyl/interfaces'

import { $bind, update } from "@sp2/updater"
import { DocumentPath, getNestedValue } from '@sp2/format'

import { EncryptFunction } from './decls'

export function encryptPasswordInRequestData(reqData: GeneralRequestData, passwordPropName: DocumentPath, encrypt: EncryptFunction): GeneralRequestData {
  switch (reqData.method) {
    case 'insertOne':
    case 'insertAndGet': {
      const { payload } = reqData
      const { value } = payload
      const password = getNestedValue(value, passwordPropName)

      if (password) {
        const { $set, $docPath } = $bind<typeof value>()
        const valueWithEncryptedPass = update(value, $set($docPath(passwordPropName), encrypt(password)))
        const { $set: $OtherSet, $docPath: $otherDocPath } = $bind<typeof reqData>() 
        return update(reqData, $OtherSet($otherDocPath("payload", "value"), valueWithEncryptedPass))
      } else {
        return reqData
      }
    }
    case 'insertMulti':<commit_msg>migrate to sp2<commit_after>import {
  GeneralRequestData,
} from '@phenyl/interfaces'

import { 
  $bind,
  update,
  DocumentPath,
  getNestedValue
} from "sp2"

import { EncryptFunction } from './decls'

export function encryptPasswordInRequestData(reqData: GeneralRequestData, passwordPropName: DocumentPath, encrypt: EncryptFunction): GeneralRequestData {
  switch (reqData.method) {
    case 'insertOne':
    case 'insertAndGet': {
      const { payload } = reqData
      const { value } = payload
      const password = getNestedValue(value, passwordPropName)

      if (password) {
        const { $set, $docPath } = $bind<typeof value>()
        const valueWithEncryptedPass = update(value, $set($docPath(passwordPropName), encrypt(password)))
        const { $set: $OtherSet, $docPath: $otherDocPath } = $bind<typeof reqData>() 
        return update(reqData, $OtherSet($otherDocPath("payload", "value"), valueWithEncryptedPass))
      } else {
        return reqData
      }
    }
    case 'insertMulti':
{'fraction_non_alphanumeric': 0.11807580174927114, 'fraction_numerical': 0.001943634596695821, 'mean_word_length': 3.8907363420427554, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3519278', 'n_tokens_mistral': 608, 'n_tokens_neox': 594, 'n_words': 142}