-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservermonoproject.js
More file actions
279 lines (225 loc) Β· 7.88 KB
/
servermonoproject.js
File metadata and controls
279 lines (225 loc) Β· 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Replace your server.js with the enhanced debug version
cat > server.js << 'EOF'
import express from 'express';
import cors from 'cors';
import { config } from 'dotenv';
config();
const app = express();
const PORT = 3001;
app.use(cors({
origin: ['http://localhost:3000', 'http://localhost:3001'],
credentials: true
}));
app.use(express.json());
const makeAsanaRequest = async (endpoint, method = 'GET', data = null) => {
const { default: fetch } = await import('node-fetch');
console.log(`π ${method} ${endpoint}`);
if (data) console.log('π€ Data:', JSON.stringify(data, null, 2));
const options = {
method,
headers: {
'Authorization': `Bearer ${process.env.VITE_ASANA_TOKEN}`,
'Accept': 'application/json',
'Content-Type': 'application/json'
}
};
if (data && method !== 'GET') {
options.body = JSON.stringify({ data });
}
const response = await fetch(`https://app.asana.com/api/1.0${endpoint}`, options);
const result = await response.json();
console.log(`π₯ Response Status: ${response.status}`);
if (!response.ok) {
console.error('β Asana API Error:', result);
throw new Error(`Asana API Error: ${response.status} - ${result.errors?.[0]?.message || 'Unknown error'}`);
}
return result;
};
// User and workspace endpoints
app.get('/api/users/me', async (req, res) => {
try {
const data = await makeAsanaRequest('/users/me');
res.json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.get('/api/workspaces', async (req, res) => {
try {
const data = await makeAsanaRequest('/workspaces');
res.json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Get projects
app.get('/api/projects', async (req, res) => {
try {
const { workspace } = req.query;
const endpoint = `/projects?workspace=${workspace}&opt_fields=name,color,created_at,modified_at,owner.name,archived,notes`;
const data = await makeAsanaRequest(endpoint);
res.json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// CREATE PROJECT - The problematic endpoint
app.post('/api/projects', async (req, res) => {
try {
console.log('π― CREATE PROJECT REQUEST RECEIVED');
console.log('π₯ Full request body:', JSON.stringify(req.body, null, 2));
const { name, notes, color, workspace } = req.body;
if (!name) {
console.log('β Missing project name');
return res.status(400).json({ error: 'Project name is required' });
}
if (!workspace) {
console.log('β Missing workspace');
return res.status(400).json({ error: 'Workspace is required' });
}
// Create minimal project data (start simple)
const projectData = {
name: name.trim(),
workspace: workspace
};
// Add notes if provided
if (notes && notes.trim()) {
projectData.notes = notes.trim();
}
console.log('π€ Sending to Asana:', JSON.stringify(projectData, null, 2));
const data = await makeAsanaRequest('/projects', 'POST', projectData);
console.log('β
SUCCESS! Project created');
res.json(data);
} catch (error) {
console.error('β CREATE PROJECT ERROR:', error.message);
console.error('β Full error:', error);
res.status(500).json({ error: error.message });
}
});
// Get tasks
app.get('/api/tasks', async (req, res) => {
try {
const { project } = req.query;
const endpoint = `/tasks?project=${project}&opt_fields=name,completed,assignee.name,due_date,created_at,notes`;
const data = await makeAsanaRequest(endpoint);
res.json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Toggle task completion
app.put('/api/tasks/:taskId/toggle', async (req, res) => {
try {
const { taskId } = req.params;
const { completed } = req.body;
const data = await makeAsanaRequest(`/tasks/${taskId}`, 'PUT', { completed });
res.json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Get workspace users
app.get('/api/workspaces/:workspaceId/users', async (req, res) => {
try {
const { workspaceId } = req.params;
const data = await makeAsanaRequest(`/workspaces/${workspaceId}/users?opt_fields=name,email`);
res.json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(PORT, () => {
console.log('π Debug Server Started!');
console.log(`π http://localhost:${PORT}`);
console.log(`π Token: ${process.env.VITE_ASANA_TOKEN ? 'Present' : 'Missing'}`);
console.log('π Debug mode: ON');
});
EOF
// ========== TASK ENDPOINTS ==========
// Create task
app.post('/api/tasks', async (req, res) => {
try {
console.log('π― CREATE TASK REQUEST');
console.log('π₯ Request body:', JSON.stringify(req.body, null, 2));
const { name, notes, due_date, assignee, projects } = req.body;
if (!name) {
return res.status(400).json({ error: 'Task name is required' });
}
if (!projects) {
return res.status(400).json({ error: 'Project is required' });
}
const taskData = {
name: name.trim(),
projects: [projects] // Asana expects an array
};
// Add optional fields with proper formatting
if (notes && notes.trim()) {
taskData.notes = notes.trim();
}
if (due_date && due_date.trim()) {
// Asana expects YYYY-MM-DD format
taskData.due_date = due_date.trim();
console.log('π
Setting due date:', taskData.due_date);
}
if (assignee && assignee.trim()) {
taskData.assignee = assignee.trim();
}
console.log('π€ Task data to Asana:', JSON.stringify(taskData, null, 2));
const data = await makeAsanaRequest('/tasks', 'POST', taskData);
console.log('β
Task created successfully!');
res.json(data);
} catch (error) {
console.error('β Create task error:', error.message);
res.status(500).json({ error: error.message });
}
});
// Update task
app.put('/api/tasks/:taskId', async (req, res) => {
try {
console.log('π― UPDATE TASK REQUEST');
const { taskId } = req.params;
console.log('π Task ID:', taskId);
console.log('π₯ Request body:', JSON.stringify(req.body, null, 2));
const { name, notes, due_date, completed, assignee } = req.body;
const updateData = {};
if (name !== undefined) updateData.name = name;
if (notes !== undefined) updateData.notes = notes;
if (completed !== undefined) updateData.completed = completed;
// Handle due date with proper formatting
if (due_date !== undefined) {
if (due_date && due_date.trim()) {
// Ensure YYYY-MM-DD format
updateData.due_date = due_date.trim();
console.log('π
Updating due date to:', updateData.due_date);
} else {
// Clear due date if empty string
updateData.due_date = null;
console.log('π
Clearing due date');
}
}
if (assignee !== undefined) {
updateData.assignee = assignee || null;
}
console.log('π€ Update data to Asana:', JSON.stringify(updateData, null, 2));
const data = await makeAsanaRequest(`/tasks/${taskId}`, 'PUT', updateData);
console.log('β
Task updated successfully!');
res.json(data);
} catch (error) {
console.error('β Update task error:', error.message);
console.error('β Full error:', error);
res.status(500).json({ error: error.message });
}
});
// Delete task
app.delete('/api/tasks/:taskId', async (req, res) => {
try {
const { taskId } = req.params;
console.log('ποΈ Deleting task:', taskId);
const data = await makeAsanaRequest(`/tasks/${taskId}`, 'DELETE');
console.log('β
Task deleted successfully!');
res.json(data);
} catch (error) {
console.error('β Delete task error:', error.message);
res.status(500).json({ error: error.message });
}
});