|
| 1 | +import type { |
| 2 | + TEndorsePersonSkillsParams, |
| 3 | + TPersonSkill, |
| 4 | + TPersonSkillEndorsement, |
| 5 | +} from '../types/actions'; |
| 6 | +import { LinkedApiError } from '../types/errors'; |
| 7 | +import type { |
| 8 | + TWorkflowCompletion, |
| 9 | + TWorkflowDefinition, |
| 10 | + TWorkflowSingleData, |
| 11 | +} from '../types/workflows'; |
| 12 | + |
| 13 | +import { BaseMapper, TMappedResponse } from './base-mapper.abstract'; |
| 14 | + |
| 15 | +type TSkillCompletion = TPersonSkill & TWorkflowSingleData; |
| 16 | + |
| 17 | +export class EndorsePersonSkillsMapper extends BaseMapper< |
| 18 | + TEndorsePersonSkillsParams, |
| 19 | + Array<TPersonSkillEndorsement> |
| 20 | +> { |
| 21 | + public override mapRequest(params: TEndorsePersonSkillsParams): TWorkflowDefinition { |
| 22 | + const { personUrl, skillNamePatterns } = params; |
| 23 | + |
| 24 | + return { |
| 25 | + actionType: 'st.openPersonPage', |
| 26 | + personUrl, |
| 27 | + basicInfo: false, |
| 28 | + then: { |
| 29 | + actionType: 'st.retrievePersonSkills', |
| 30 | + then: { |
| 31 | + actionType: 'st.doForSkills', |
| 32 | + ...(skillNamePatterns !== undefined ? { skillNamePatterns } : {}), |
| 33 | + then: { |
| 34 | + actionType: 'st.endorsePersonSkill', |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + public override mapResponse( |
| 42 | + completion: TWorkflowCompletion, |
| 43 | + ): TMappedResponse<Array<TPersonSkillEndorsement>> { |
| 44 | + const root = Array.isArray(completion) ? completion[0] : completion; |
| 45 | + if (!root) { |
| 46 | + throw LinkedApiError.unknownError(); |
| 47 | + } |
| 48 | + if (root.error) { |
| 49 | + return { errors: [root.error] }; |
| 50 | + } |
| 51 | + |
| 52 | + const then = Array.isArray(root.data) ? undefined : root.data?.then; |
| 53 | + const skillsAction = Array.isArray(then) |
| 54 | + ? then.find((action) => action.actionType === 'st.retrievePersonSkills') |
| 55 | + : then; |
| 56 | + if (!skillsAction || skillsAction.actionType !== 'st.retrievePersonSkills') { |
| 57 | + throw LinkedApiError.unknownError(); |
| 58 | + } |
| 59 | + if (skillsAction.error) { |
| 60 | + return { errors: [skillsAction.error] }; |
| 61 | + } |
| 62 | + if (!Array.isArray(skillsAction.data)) { |
| 63 | + throw LinkedApiError.unknownError(); |
| 64 | + } |
| 65 | + |
| 66 | + const skills = skillsAction.data as Array<TSkillCompletion>; |
| 67 | + const data: Array<TPersonSkillEndorsement> = []; |
| 68 | + for (const skill of skills) { |
| 69 | + const endorsement = Array.isArray(skill.then) |
| 70 | + ? skill.then.find((action) => action.actionType === 'st.endorsePersonSkill') |
| 71 | + : skill.then; |
| 72 | + if (!endorsement || endorsement.actionType !== 'st.endorsePersonSkill') { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + data.push({ |
| 77 | + skillName: skill.name, |
| 78 | + success: endorsement.success, |
| 79 | + ...(endorsement.error ? { error: endorsement.error } : {}), |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + return { |
| 84 | + data, |
| 85 | + errors: [], |
| 86 | + }; |
| 87 | + } |
| 88 | +} |
0 commit comments