Skip to content

Commit 2115b28

Browse files
authored
Create template-generator.js
1 parent 7abfe45 commit 2115b28

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

asana/template-generator.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Run this after creating a sample task in the target project.
2+
// Paste that task's gid below.
3+
const SAMPLE_TASK_GID = "1210973692586457";
4+
5+
// 1) Standard fields (present across projects)
6+
const STANDARD_FIELDS = {
7+
SubmissionType: "1202329899911645",
8+
ReportFormat: "1202330347491974",
9+
CaseID: "1202329899911595",
10+
GrievanceID: "1201884379104074",
11+
Gender: "1202330737362426",
12+
IndigenousPeoples: "1202330755979944",
13+
Age: "1202330714895606"
14+
};
15+
16+
// 2) Standard option GIDs (enum choices)
17+
const STANDARD_OPTION_MAPPING = {
18+
"SubmissionType_Grievance": "1202329899911646",
19+
"SubmissionType_Suggestion": "1202329899911647",
20+
"SubmissionType_Request": "1202329899911648",
21+
"SubmissionType_Feedback": "1202329899911649",
22+
23+
"ReportFormat_InPerson": "1202330347493011",
24+
"ReportFormat_FocalPoint": "1207724960884497",
25+
"ReportFormat_VoiceCall": "1202330347494027",
26+
"ReportFormat_Hotline": "1202330347501419",
27+
"ReportFormat_TextMessage": "1202330347498273",
28+
"ReportFormat_SuggestionBox": "1202330347499327",
29+
"ReportFormat_Email": "1202330347502485",
30+
"ReportFormat_Letter": "1202330347503544",
31+
"ReportFormat_OnlineForm": "1207934414764297",
32+
"ReportFormat_Other": "1203830536105154",
33+
"ReportFormat_Prospecting": "1208419056473280",
34+
35+
"Gender_Male": "1202330737362427",
36+
"Gender_Female": "1202330737362428",
37+
"Gender_Mixed gender": "1202330737362429",
38+
"Gender_Unknown gender": "1202330737362430",
39+
"Gender_Other gender": "1207852335266397",
40+
"Gender_Prefer not to report": "1207852335266398",
41+
42+
"IndigenousPeoples_Yes": "1202330755980982",
43+
"IndigenousPeoples_No": "1202330755984093",
44+
"IndigenousPeoples_Unknown": "1202330755985164",
45+
"IndigenousPeoples_Mixed group": "1207724962870243",
46+
47+
"Age_<18": "1202330714895607",
48+
"Age_19–35": "1202330714895608",
49+
"Age_36–50": "1202330714895609",
50+
"Age_>50": "1202330714895610",
51+
"Age_Mixed age": "1202330714895611",
52+
"Age_Unknown age": "1202330714895612"
53+
};
54+
55+
// Fetch the sample task so we can inspect this project's custom fields
56+
getTask(SAMPLE_TASK_GID, {}, state => {
57+
const { data } = state.body;
58+
59+
// Build a set of standard field GIDs so we can skip duplicates later
60+
const standardGids = new Set(Object.values(STANDARD_FIELDS));
61+
const presentGids = new Set(data.custom_fields.map(f => f.gid));
62+
63+
// 3) Build the combined mapping table: start with standard options, then add project-specific options
64+
const formatMapping = { ...STANDARD_OPTION_MAPPING };
65+
66+
data.custom_fields.forEach(field => {
67+
// Skip if this field is one of the known standard fields
68+
if (standardGids.has(field.gid)) return;
69+
70+
if (field.enum_options) {
71+
field.enum_options.forEach(option => {
72+
formatMapping[`${field.name}_${option.name}`] = option.gid;
73+
});
74+
}
75+
});
76+
77+
// Print a copy-paste friendly mapping table
78+
console.log("//======= Mapping Table (Standard + Project specific) =========");
79+
console.log("-----BEGIN PASTE 1-----");
80+
console.log("const formatMapping = " + JSON.stringify(formatMapping, null, 2) + ";");
81+
console.log("state.formatMapping = formatMapping;");
82+
console.log("-----END PASTE 1-----");
83+
console.log("//===================== End Mapping Table =====================");
84+
console.log("");
85+
86+
87+
// 4) Print statements for custom_fields payload
88+
// helpers
89+
const cleanName = s => (s || '').trim();
90+
const isIdent = s => /^[A-Za-z_$][\w$]*$/.test(s);
91+
const bodyRef = name => {
92+
const clean = cleanName(name);
93+
return isIdent(clean) ? `$.inputData.body.${clean}` : `$.inputData.body[${JSON.stringify(clean)}]`;
94+
};
95+
const enumExpr = name => {
96+
const clean = cleanName(name);
97+
// lookup key is "<FieldName>_" + raw payload value
98+
return `state => state.formatMapping[${JSON.stringify(clean + "_")} + ${bodyRef(clean)}]`;
99+
};
100+
101+
let paste2 = "custom_fields: {\n";
102+
103+
// Standard open-ended
104+
if (presentGids.has(STANDARD_FIELDS.CaseID)) {
105+
paste2 += ` '${STANDARD_FIELDS.CaseID}': ${bodyRef('CaseID')},\n`;
106+
}
107+
if (presentGids.has(STANDARD_FIELDS.GrievanceID)) {
108+
paste2 += ` '${STANDARD_FIELDS.GrievanceID}': ${bodyRef('GrievanceID')},\n`;
109+
}
110+
111+
// Standard enums
112+
['SubmissionType', 'ReportFormat', 'Gender', 'IndigenousPeoples', 'Age'].forEach(name => {
113+
const gid = STANDARD_FIELDS[name];
114+
if (presentGids.has(gid)) {
115+
paste2 += ` '${gid}': ${enumExpr(name)},\n`;
116+
}
117+
});
118+
119+
// Project specific
120+
data.custom_fields.forEach(f => {
121+
if (standardGids.has(f.gid)) return;
122+
if (f.name && !f.enum_options) {
123+
paste2 += ` '${f.gid}': ${bodyRef(f.name)},\n`;
124+
} else if (f.name && f.enum_options) {
125+
paste2 += ` '${f.gid}': ${enumExpr(f.name)},\n`;
126+
}
127+
});
128+
129+
paste2 += "}";
130+
131+
console.log("-----BEGIN PASTE 2-----\n" + paste2 + "\n-----END PASTE 2-----");
132+
console.log("//=========== End of Statements =======================");
133+
134+
// Return state with the combined mapping in case you want to consume it programmatically
135+
return { ...state, formatMapping };
136+
});

0 commit comments

Comments
 (0)