···
27
+
interface ImpactIndexerProposal {
28
+
$type: 'org.impactindexer.proposal'
30
+
displayName?: string
31
+
description?: string
33
+
fundingReceived?: number
34
+
fundingGivenOut?: number
35
+
annualBudget?: number
37
+
sustainableRevenuePercent?: number
38
+
categories?: string[]
39
+
impactMetrics?: string
40
+
geographicDistribution?: string
async function getSessionAgent(): Promise<Agent | null> {
const session = await getSession()
···
const body = await request.json()
148
+
const { targetDid, isProposal, ...recordData } = body
console.log('Debug - POST request body:', body)
console.log('Debug - Website in request body:', body.website)
152
+
console.log('Debug - isProposal:', isProposal, 'targetDid:', targetDid)
134
-
// Check if there's an existing record
135
-
let existingRecord = null
136
-
let rkey = TID.nextStr()
154
+
const now = new Date().toISOString()
139
-
const listResponse = await agent.com.atproto.repo.listRecords({
140
-
repo: agent.assertDid,
141
-
collection: 'org.impactindexer.status',
145
-
if (listResponse.data.records.length > 0) {
146
-
existingRecord = listResponse.data.records[0]
147
-
// Extract rkey from URI for updates
148
-
rkey = existingRecord.uri.split('/').pop() || TID.nextStr()
156
+
if (isProposal && targetDid) {
157
+
// This is a proposal for someone else's project
158
+
// Save as a proposal record in the current user's repo
159
+
const proposalRecord = {
160
+
$type: 'org.impactindexer.proposal',
162
+
displayName: recordData.displayName,
163
+
description: recordData.description,
164
+
website: recordData.website,
165
+
fundingReceived: recordData.fundingReceived,
166
+
fundingGivenOut: recordData.fundingGivenOut,
167
+
annualBudget: recordData.annualBudget,
168
+
teamSize: recordData.teamSize,
169
+
sustainableRevenuePercent: recordData.sustainableRevenuePercent,
170
+
categories: recordData.categories,
171
+
impactMetrics: recordData.impactMetrics ? JSON.stringify(recordData.impactMetrics) : undefined,
172
+
geographicDistribution: recordData.geographicDistribution ? JSON.stringify(recordData.geographicDistribution) : undefined,
151
-
console.log('No existing record found, will create new one')
154
-
// Create the record
155
-
const now = new Date().toISOString()
157
-
$type: 'org.impactindexer.status',
158
-
displayName: body.displayName,
159
-
description: body.description,
160
-
website: body.website,
161
-
fundingReceived: body.fundingReceived,
162
-
fundingGivenOut: body.fundingGivenOut,
163
-
annualBudget: body.annualBudget,
164
-
teamSize: body.teamSize,
165
-
sustainableRevenuePercent: body.sustainableRevenuePercent,
166
-
categories: body.categories,
167
-
impactMetrics: body.impactMetrics ? JSON.stringify(body.impactMetrics) : undefined,
168
-
geographicDistribution: body.geographicDistribution ? JSON.stringify(body.geographicDistribution) : undefined,
169
-
createdAt: (existingRecord?.value as any)?.createdAt || now,
173
-
console.log('Debug - Final record to save:', record)
174
-
console.log('Debug - Website in final record:', record.website)
177
+
// Remove undefined fields
178
+
Object.keys(proposalRecord).forEach(key => {
179
+
if ((proposalRecord as any)[key] === undefined) {
180
+
delete (proposalRecord as any)[key]
176
-
// Remove undefined fields
177
-
Object.keys(record).forEach(key => {
178
-
if ((record as any)[key] === undefined) {
179
-
delete (record as any)[key]
185
+
const rkey = TID.nextStr()
186
+
const response = await agent.com.atproto.repo.putRecord({
187
+
repo: agent.assertDid,
188
+
collection: 'org.impactindexer.proposal',
190
+
record: proposalRecord,
194
+
return NextResponse.json({
197
+
uri: response.data.uri,
198
+
cid: response.data.cid
201
+
console.error('Failed to write proposal record:', error)
202
+
return NextResponse.json({ error: 'Failed to save proposal' }, { status: 500 })
205
+
// This is a direct update to the user's own project
206
+
// Check if there's an existing record
207
+
let existingRecord = null
208
+
let rkey = TID.nextStr()
211
+
const listResponse = await agent.com.atproto.repo.listRecords({
212
+
repo: agent.assertDid,
213
+
collection: 'org.impactindexer.status',
217
+
if (listResponse.data.records.length > 0) {
218
+
existingRecord = listResponse.data.records[0]
219
+
// Extract rkey from URI for updates
220
+
rkey = existingRecord.uri.split('/').pop() || TID.nextStr()
223
+
console.log('No existing record found, will create new one')
226
+
// Create the record
228
+
$type: 'org.impactindexer.status',
229
+
displayName: recordData.displayName,
230
+
description: recordData.description,
231
+
website: recordData.website,
232
+
fundingReceived: recordData.fundingReceived,
233
+
fundingGivenOut: recordData.fundingGivenOut,
234
+
annualBudget: recordData.annualBudget,
235
+
teamSize: recordData.teamSize,
236
+
sustainableRevenuePercent: recordData.sustainableRevenuePercent,
237
+
categories: recordData.categories,
238
+
impactMetrics: recordData.impactMetrics ? JSON.stringify(recordData.impactMetrics) : undefined,
239
+
geographicDistribution: recordData.geographicDistribution ? JSON.stringify(recordData.geographicDistribution) : undefined,
240
+
createdAt: (existingRecord?.value as any)?.createdAt || now,
183
-
// Basic validation
184
-
if (!record.createdAt) {
185
-
return NextResponse.json({ error: 'Invalid project status data: createdAt required' }, { status: 400 })
244
+
console.log('Debug - Final record to save:', record)
245
+
console.log('Debug - Website in final record:', record.website)
188
-
// Save the record to ATProto
190
-
const response = await agent.com.atproto.repo.putRecord({
191
-
repo: agent.assertDid,
192
-
collection: 'org.impactindexer.status',
198
-
return NextResponse.json({
200
-
uri: response.data.uri,
201
-
cid: response.data.cid
247
+
// Remove undefined fields
248
+
Object.keys(record).forEach(key => {
249
+
if ((record as any)[key] === undefined) {
250
+
delete (record as any)[key]
204
-
console.error('Failed to write project status record:', error)
205
-
return NextResponse.json({ error: 'Failed to save project status' }, { status: 500 })
254
+
// Basic validation
255
+
if (!record.createdAt) {
256
+
return NextResponse.json({ error: 'Invalid project status data: createdAt required' }, { status: 400 })
259
+
// Save the record to ATProto
261
+
const response = await agent.com.atproto.repo.putRecord({
262
+
repo: agent.assertDid,
263
+
collection: 'org.impactindexer.status',
269
+
return NextResponse.json({
272
+
uri: response.data.uri,
273
+
cid: response.data.cid
276
+
console.error('Failed to write project status record:', error)
277
+
return NextResponse.json({ error: 'Failed to save project status' }, { status: 500 })
console.error('Project status update failed:', error)