Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 66 additions & 8 deletions backend/src/api/project/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { cleanupProject, generateProjectName } from '@backend/api/project/project.import-file'
import {
AffectedRowsSchema,
ColumnNameSchema,
GetProjectByIdResponse,
PaginationQuery,
ProjectParams,
ProjectResponseSchema,
ReplaceOperationSchema,
TrimWhitespaceSchema,
type Project,
} from '@backend/api/project/schemas'
import { databasePlugin } from '@backend/plugins/database'
import { errorHandlerPlugin } from '@backend/plugins/error-handler'
import { ReplaceOperationService } from '@backend/services/replace-operation.service'
import { TrimWhitespaceService } from '@backend/services/trim-whitespace.service'
import { UppercaseConversionService } from '@backend/services/uppercase-conversion.service'
import { ApiErrorHandler } from '@backend/types/error-handler'
import { ApiErrors } from '@backend/types/error-schemas'
import { enhanceSchemaWithTypes, type DuckDBTablePragma } from '@backend/utils/duckdb-types'
Expand Down Expand Up @@ -585,9 +587,7 @@ export const projectRoutes = new Elysia({ prefix: '/api/project' })
{
body: ReplaceOperationSchema,
response: {
200: t.Object({
affectedRows: t.Integer(),
}),
200: AffectedRowsSchema,
400: ApiErrors,
404: ApiErrors,
422: ApiErrors,
Expand Down Expand Up @@ -644,11 +644,9 @@ export const projectRoutes = new Elysia({ prefix: '/api/project' })
}
},
{
body: TrimWhitespaceSchema,
body: ColumnNameSchema,
response: {
200: t.Object({
affectedRows: t.Integer(),
}),
200: AffectedRowsSchema,
400: ApiErrors,
404: ApiErrors,
422: ApiErrors,
Expand All @@ -662,3 +660,63 @@ export const projectRoutes = new Elysia({ prefix: '/api/project' })
},
},
)

.post(
'/:projectId/uppercase',
async ({ db, params: { projectId }, body: { column }, status }) => {
const table = `project_${projectId}`

// Check if column exists
const columnExistsReader = await db().runAndReadAll(
'SELECT 1 FROM information_schema.columns WHERE table_name = ? AND column_name = ?',
[table, column],
)

if (columnExistsReader.getRows().length === 0) {
return status(
400,
ApiErrorHandler.validationErrorWithData('Column not found', [
`Column '${column}' does not exist in table '${table}'`,
]),
)
}

const uppercaseConversionService = new UppercaseConversionService(db())

try {
const affectedRows = await uppercaseConversionService.performOperation({
table,
column,
})

return {
affectedRows,
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'
return status(
500,
ApiErrorHandler.internalServerErrorWithData(
'Failed to perform uppercase conversion operation',
[errorMessage],
),
)
}
},
{
body: ColumnNameSchema,
response: {
200: AffectedRowsSchema,
400: ApiErrors,
404: ApiErrors,
422: ApiErrors,
500: ApiErrors,
},
detail: {
summary: 'Convert text to uppercase in a column',
description:
'Convert all text values in a specific column to uppercase',
tags,
},
},
)
21 changes: 11 additions & 10 deletions backend/src/api/project/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,21 @@ export const GetProjectByIdResponse = t.Object({
})
export type GetProjectByIdResponse = typeof GetProjectByIdResponse.static

// Replace operation schema
export const ReplaceOperationSchema = t.Object({
// Column operation schema
export const ColumnNameSchema = t.Object({
column: t.String({
minLength: 1,
error: 'Column name is required and must be at least 1 character long',
}),
})

export const AffectedRowsSchema = t.Object({
affectedRows: t.Integer(),
})

// Replace operation schema
export const ReplaceOperationSchema = t.Object({
column: ColumnNameSchema.properties.column,
find: t.String({
minLength: 1,
error: 'Find value is required and must be at least 1 character long',
Expand All @@ -70,11 +79,3 @@ export const ReplaceOperationSchema = t.Object({
default: false,
}),
})

// Trim whitespace operation schema
export const TrimWhitespaceSchema = t.Object({
column: t.String({
minLength: 1,
error: 'Column name is required and must be at least 1 character long',
}),
})
43 changes: 43 additions & 0 deletions backend/src/services/uppercase-conversion.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { ColumnOperationParams } from '@backend/services/column-operation.service'
import { ColumnOperationService } from '@backend/services/column-operation.service'

export class UppercaseConversionService extends ColumnOperationService {
public async performOperation(params: ColumnOperationParams): Promise<number> {
const { table, column } = params

return this.executeColumnOperation(
table,
column,
() => this.buildParameterizedUpdateQuery(table, column),
() => this.countAffectedRows(table, column),
)
}

/**
* Builds a parameterized UPDATE query to safely perform uppercase conversion operations
*/
private buildParameterizedUpdateQuery(table: string, column: string) {
const query = `
UPDATE "${table}"
SET "${column}" = UPPER("${column}")
WHERE "${column}" IS NOT NULL
AND "${column}" != UPPER("${column}")
`

return { query, params: [] }
}

/**
* Counts the number of rows that would be affected by the uppercase conversion operation
*/
private countAffectedRows(table: string, column: string): Promise<number> {
const query = `
SELECT COUNT(*) as count
FROM "${table}"
WHERE "${column}" IS NOT NULL
AND "${column}" != UPPER("${column}")
`

return this.getCount(query, [])
}
}
8 changes: 5 additions & 3 deletions backend/tests/api/project/project.replace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('Project API - find and replace', () => {
await closeDb()
})

test('should perform basic replace operation', async () => {
test('should perform basic find and replace operation', async () => {
const { data, status, error } = await api.project({ projectId }).replace.post({
column: 'city',
find: 'New York',
Expand Down Expand Up @@ -265,7 +265,7 @@ describe('Project API - find and replace', () => {
replace: "Jonathan's",
expectedAffectedRows: 0, // No data with John's in test data
},
])('$description', async ({ column, find, replace }) => {
])('$description', async ({ column, find, replace, expectedAffectedRows }) => {
const { data, status, error } = await api.project({ projectId }).replace.post({
column,
find,
Expand All @@ -276,7 +276,9 @@ describe('Project API - find and replace', () => {

expect(status).toBe(200)
expect(error).toBeNull()
expect(data!.affectedRows).toBeGreaterThanOrEqual(0)
expect(data).toEqual({
affectedRows: expectedAffectedRows,
})
})
})
})
121 changes: 121 additions & 0 deletions backend/tests/api/project/uppercase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { projectRoutes } from '@backend/api/project'
import { closeDb, initializeDb } from '@backend/plugins/database'
import { treaty } from '@elysiajs/eden'
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
import { Elysia } from 'elysia'
import { tmpdir } from 'node:os'

interface TestData {
name: string
email: string
city: string
}

const TEST_DATA: TestData[] = [
{ name: 'John Doe', email: 'john@example.com', city: 'New York' },
{ name: 'Jane Smith', email: 'jane@example.com', city: 'Los Angeles' },
{ name: 'Bob Johnson', email: 'bob@example.com', city: 'New York' },
{ name: 'Alice Brown', email: 'alice@test.com', city: 'Chicago' },
{ name: 'Charlie Davis', email: 'charlie@example.com', city: 'New York' },
]

const createTestApi = () => {
return treaty(new Elysia().use(projectRoutes)).api
}

const tempFilePath = tmpdir() + '/test-data.json'

describe('Project API - Uppercase Conversion', () => {
let api: ReturnType<typeof createTestApi>
let projectId: string

const importTestData = async () => {
await Bun.write(tempFilePath, JSON.stringify(TEST_DATA))

const { status, error } = await api.project({ projectId }).import.post({
filePath: tempFilePath,
})

expect(error).toBeNull()
expect(status).toBe(201)
}

beforeEach(async () => {
await initializeDb(':memory:')
api = createTestApi()

const { data, status, error } = await api.project.post({
name: 'Test Project for uppercase',
})
expect(error).toBeNull()
expect(status).toBe(201)
projectId = (data as any)!.data!.id as string

await importTestData()
})

afterEach(async () => {
await closeDb()
})

test('should perform basic uppercase conversion', async () => {
const { data, status, error } = await api.project({ projectId }).uppercase.post({
column: 'name',
})

expect(status).toBe(200)
expect(error).toBeNull()
expect(data).toEqual({
affectedRows: 5,
})

// Verify the data was actually changed
const { data: projectData } = await api.project({ projectId }).get({
query: { offset: 0, limit: 25 },
})

expect(projectData).toHaveProperty(
'data',
expect.arrayContaining([
expect.objectContaining({ name: 'JOHN DOE' }),
expect.objectContaining({ name: 'JANE SMITH' }),
expect.objectContaining({ name: 'BOB JOHNSON' }),
expect.objectContaining({ name: 'ALICE BROWN' }),
expect.objectContaining({ name: 'CHARLIE DAVIS' }),
]),
)
})

test('should return 400 for non-existent column', async () => {
const { data, status, error } = await api.project({ projectId }).uppercase.post({
column: 'nonexistent_column',
})

expect(status).toBe(400)
expect(data).toBeNull()
expect(error).toHaveProperty('status', 400)
expect(error).toHaveProperty('value', [
{
code: 'VALIDATION',
message: 'Column not found',
details: [`Column 'nonexistent_column' does not exist in table 'project_${projectId}'`],
},
])
})

test('should return 422 for missing required fields', async () => {
const { data, status, error } = await api.project({ projectId }).uppercase.post({
column: '',
})

expect(status).toBe(422)
expect(data).toBeNull()
expect(error).toHaveProperty('status', 422)
expect(error).toHaveProperty('value', expect.arrayContaining([
expect.objectContaining({
message: 'Expected string length greater or equal to 1',
path: '/column',
}),
]))
})
})
Loading