cloud-action-migration.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Actions that are safe to route from the current Angular H5 application to
  3. * the new Parse cloud-function gateway. Keep this list narrower than the
  4. * server-side mapping until each page's response shape has passed regression.
  5. */
  6. export const H5_CLOUD_ROUTED_ACTIONS = new Set([
  7. 'app_update',
  8. 'node_get',
  9. 'node_list',
  10. 'user_login_passwd',
  11. ]);
  12. const SESSION_CLOUD_ROUTED_ACTIONS = new Set([
  13. 'e_add_words',
  14. 'e_ck_list',
  15. 'e_get_21list',
  16. 'e_order_detail',
  17. 'e_order_tongji',
  18. 'e_record_detail',
  19. 'e_words_list',
  20. 'stu_record_update_v2',
  21. ]);
  22. const CONTENT_MODEL_FIELDS: Record<number, Set<string>> = {
  23. 52: new Set(['id', 'lj', 'sy', 'yb', 'yp']),
  24. 53: new Set(['id', 'kcid', 'scid', 'xxcs', 'yhid', 'jrscb']),
  25. 54: new Set(['id', 'pl', 'bxrq', 'dslx', 'dszt', 'fxpl', 'jffs', 'jssj', 'kcid', 'kssj', 'plxm', 'scsj', 'sdsd', 'szmd', 'szyh', 'yysj', 'yykcid']),
  26. 56: new Set(['id', 'pl', 'con', 'djq', 'ygg', 'dqrq', 'dsid', 'fxrl', 'xxqs', 'szmdid', 'userid', 'learned']),
  27. 58: new Set(['id', 'yxx', 'cksl', 'kcid', 'syjd', 'yhid']),
  28. 59: new Set(['id', 'pf', 'jffs', 'jsmz', 'kcid', 'kclx', 'kcmc', 'kzsj', 'pjnr', 'pldp', 'xymz', 'yyds', 'plpjsj', 'szmdid']),
  29. 60: new Set(['id', 'fxzt', 'kcid', 'plid', 'wcsj', 'yhid', 'kywrq', 'kywsj', 'xxjlid', 'orderid']),
  30. 61: new Set(['id', 'df', 'askid', 'wrong', 'userid', 'answerid', 'dontknow', 'prevscore', 'totalscore']),
  31. };
  32. const NODE_MODELS: Record<number, number> = { 28: 58, 29: 54, 32: 53, 291: 56, 296: 59, 327: 52, 388: 60, 389: 61 };
  33. export function usesCloudGateway(action: string, params: Record<string, unknown> = {}, hasParseSession = false): boolean {
  34. if (H5_CLOUD_ROUTED_ACTIONS.has(action)) return true;
  35. if (SESSION_CLOUD_ROUTED_ACTIONS.has(action)) return hasParseSession;
  36. if (action !== 'content_list') return false;
  37. const modelId = contentModel(params);
  38. if (!modelId) return !params['myfield'] && !params['myfield2'];
  39. const fields = CONTENT_MODEL_FIELDS[modelId];
  40. if (!fields || (modelId !== 52 && !hasParseSession)) return false;
  41. return filterNames(params).every((field) => fields.has(field));
  42. }
  43. export function requiresCloudSession(action: string, params: Record<string, unknown> = {}): boolean {
  44. return SESSION_CLOUD_ROUTED_ACTIONS.has(action)
  45. || (action === 'content_list' && contentModel(params) !== 52 && Boolean(contentModel(params)));
  46. }
  47. function contentModel(params: Record<string, unknown>): number {
  48. const explicit = Number(params['modelId'] ?? params['modelid'] ?? params['ModelID'] ?? 0);
  49. if (explicit) return explicit;
  50. const nodes = String(params['nodeid'] ?? params['nid'] ?? params['nodes'] ?? '').split(',').map(Number).filter(Boolean);
  51. return nodes.length === 1 ? NODE_MODELS[nodes[0]] ?? 0 : 0;
  52. }
  53. function filterNames(params: Record<string, unknown>): string[] {
  54. return [params['myfield'], params['myfield2']]
  55. .filter(Boolean)
  56. .flatMap((value) => String(value).split('|'))
  57. .map((part) => part.split('=', 1)[0].replace(/^B\./i, '').toLowerCase());
  58. }