Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit 50b2fcb

Browse files
authored
Merge pull request #23 from aibtcdev/feat/more-endpoints
Add requested endpoints
2 parents 70486b2 + b4fb822 commit 50b2fcb

File tree

6 files changed

+301
-28
lines changed

6 files changed

+301
-28
lines changed

src/database/aibtcdev.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ CREATE TABLE user_crew_execution_steps (
303303
CREATE INDEX idx_execution_steps_created_at ON user_crew_execution_steps(created_at);
304304
CREATE INDEX idx_execution_steps_profile_id ON user_crew_execution_steps(profile_id);
305305
CREATE INDEX idx_execution_steps_crew_id ON user_crew_execution_steps(crew_id);
306+
CREATE INDEX idx_execution_steps_profile_id ON user_crew_execution_steps(profile_id);
306307
CREATE INDEX idx_execution_steps_execution_id ON user_crew_execution_steps(execution_id);
307308
CREATE INDEX idx_execution_steps_type ON user_crew_execution_steps(step_type);
308309

src/database/helpers/crews.ts

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,124 @@ export async function deleteCrew(orm: D1Orm, crewId: number): Promise<CrewResult
238238
};
239239
}
240240
}
241+
242+
/**
243+
* Get all crews for a profile
244+
* @param orm The D1Orm instance from durable object class
245+
* @param address The Stacks address for the user's profile
246+
* @returns Promise containing array of crews or error details
247+
* @throws Error if database query fails
248+
*/
249+
/**
250+
* Get all execution steps for a specific execution
251+
* @param orm The D1Orm instance from durable object class
252+
* @param executionId The ID of the execution to get steps for
253+
* @returns Promise containing array of execution steps or error details
254+
*/
255+
export async function getExecutionSteps(orm: D1Orm, executionId: number): Promise<CrewResult> {
256+
try {
257+
userCrewExecutionStepsModel.SetOrm(orm);
258+
const result = await userCrewExecutionStepsModel.All({
259+
where: {
260+
execution_id: executionId
261+
},
262+
orderBy: [
263+
{
264+
column: 'created_at',
265+
ascending: true,
266+
},
267+
],
268+
});
269+
return {
270+
steps: result.results as unknown as UserCrewExecutionStepsTable[],
271+
success: true
272+
};
273+
} catch (error) {
274+
console.error(`Error in getExecutionSteps: ${error instanceof Error ? error.message : String(error)}`);
275+
return {
276+
steps: [],
277+
success: false,
278+
error: `Failed to get execution steps: ${error instanceof Error ? error.message : String(error)}`
279+
};
280+
}
281+
}
282+
283+
/**
284+
* Create a new execution step
285+
* @param orm The D1Orm instance from durable object class
286+
* @param stepData The step data to create
287+
* @returns Promise containing the created step or error details
288+
*/
289+
export async function createExecutionStep(
290+
orm: D1Orm,
291+
stepData: Omit<UserCrewExecutionStepsTable, 'id' | 'created_at' | 'updated_at'>
292+
): Promise<CrewResult> {
293+
try {
294+
userCrewExecutionStepsModel.SetOrm(orm);
295+
const step = await userCrewExecutionStepsModel.InsertOne(stepData);
296+
return {
297+
step: step as unknown as UserCrewExecutionStepsTable,
298+
success: true
299+
};
300+
} catch (error) {
301+
console.error(`Error in createExecutionStep: ${error instanceof Error ? error.message : String(error)}`);
302+
return {
303+
success: false,
304+
error: `Failed to create execution step: ${error instanceof Error ? error.message : String(error)}`
305+
};
306+
}
307+
}
308+
309+
/**
310+
* Delete all execution steps for a specific execution
311+
* @param orm The D1Orm instance from durable object class
312+
* @param executionId The ID of the execution to delete steps for
313+
* @returns Promise containing the deletion result or error details
314+
*/
315+
export async function deleteExecutionSteps(orm: D1Orm, executionId: number): Promise<CrewResult> {
316+
try {
317+
userCrewExecutionStepsModel.SetOrm(orm);
318+
await userCrewExecutionStepsModel.Delete({
319+
where: {
320+
execution_id: executionId
321+
}
322+
});
323+
return {
324+
success: true
325+
};
326+
} catch (error) {
327+
console.error(`Error in deleteExecutionSteps: ${error instanceof Error ? error.message : String(error)}`);
328+
return {
329+
success: false,
330+
error: `Failed to delete execution steps: ${error instanceof Error ? error.message : String(error)}`
331+
};
332+
}
333+
}
334+
335+
export async function getCrewsByProfile(orm: D1Orm, address: string): Promise<CrewResult> {
336+
try {
337+
userCrewsModel.SetOrm(orm);
338+
const result = await userCrewsModel.All({
339+
where: {
340+
profile_id: address
341+
},
342+
orderBy: [
343+
{
344+
column: 'created_at',
345+
descending: true,
346+
},
347+
],
348+
});
349+
return {
350+
crews: result.results as unknown as UserCrewsTable[],
351+
success: true
352+
};
353+
} catch (error) {
354+
console.error(`Error in getCrewsByProfile: ${error instanceof Error ? error.message : String(error)}`);
355+
return {
356+
crews: [],
357+
success: false,
358+
error: `Failed to get crews for profile: ${error instanceof Error ? error.message : String(error)}`
359+
};
360+
}
361+
}

src/database/models/UserCrewExecutionSteps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const userCrewExecutionStepsModel = new Model(
1010
{
1111
id: { type: DataTypes.INTEGER, notNull: true },
1212
created_at: { type: DataTypes.STRING },
13+
profile_id: { type: DataTypes.STRING, notNull: true },
1314
crew_id: { type: DataTypes.INTEGER, notNull: true },
1415
execution_id: { type: DataTypes.INTEGER, notNull: true },
1516
step_type: { type: DataTypes.STRING, notNull: true },

src/database/models/UserCrons.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const userCronsModel = new Model(
1313
updated_at: { type: DataTypes.STRING },
1414
profile_id: { type: DataTypes.STRING, notNull: true },
1515
crew_id: { type: DataTypes.INTEGER, notNull: true },
16-
cron_enabled: { type: DataTypes.INTEGER, notNull: true },
16+
cron_enabled: { type: DataTypes.BOOLEAN, notNull: true },
1717
cron_interval: { type: DataTypes.STRING, notNull: true },
1818
cron_input: { type: DataTypes.STRING, notNull: true },
1919
}

src/database/models/XBotTweets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const xBotTweetsModel = new Model(
1818
tweet_created_at: { type: DataTypes.STRING },
1919
tweet_updated_at: { type: DataTypes.STRING },
2020
tweet_body: { type: DataTypes.STRING },
21-
is_bot_response: { type: DataTypes.INTEGER },
21+
is_bot_response: { type: DataTypes.BOOLEAN },
2222
}
2323
);
2424

0 commit comments

Comments
 (0)