|
|
@ -121,38 +121,9 @@ export async function columnGet(req: Request, res: Response) { |
|
|
|
res.json(await Column.get({ colId: req.params.columnId })); |
|
|
|
res.json(await Column.get({ colId: req.params.columnId })); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function columnAdd( |
|
|
|
async function validateRollupPayload( |
|
|
|
req: Request<any, any, ColumnReqType & { uidt: UITypes }>, |
|
|
|
payload: ColumnReqType & { uidt: UITypes } |
|
|
|
res: Response<TableType> |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
const table = await Model.getWithInfo({ |
|
|
|
|
|
|
|
id: req.params.tableId, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
const base = await Base.get(table.base_id); |
|
|
|
|
|
|
|
const project = await base.getProject(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
!isVirtualCol(req.body) && |
|
|
|
|
|
|
|
!(await Column.checkTitleAvailable({ |
|
|
|
|
|
|
|
column_name: req.body.column_name, |
|
|
|
|
|
|
|
fk_model_id: req.params.tableId, |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
NcError.badRequest('Duplicate column name'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
!(await Column.checkAliasAvailable({ |
|
|
|
|
|
|
|
title: req.body.title || req.body.column_name, |
|
|
|
|
|
|
|
fk_model_id: req.params.tableId, |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
) { |
|
|
|
) { |
|
|
|
NcError.badRequest('Duplicate column alias'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let colBody: any = req.body; |
|
|
|
|
|
|
|
switch (colBody.uidt) { |
|
|
|
|
|
|
|
case UITypes.Rollup: |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
validateParams( |
|
|
|
validateParams( |
|
|
|
[ |
|
|
|
[ |
|
|
|
'title', |
|
|
|
'title', |
|
|
@ -160,12 +131,12 @@ export async function columnAdd( |
|
|
|
'fk_rollup_column_id', |
|
|
|
'fk_rollup_column_id', |
|
|
|
'rollup_function', |
|
|
|
'rollup_function', |
|
|
|
], |
|
|
|
], |
|
|
|
req.body |
|
|
|
payload |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const relation = await ( |
|
|
|
const relation = await ( |
|
|
|
await Column.get({ |
|
|
|
await Column.get({ |
|
|
|
colId: (req.body as RollupColumnReqType).fk_relation_column_id, |
|
|
|
colId: (payload as RollupColumnReqType).fk_relation_column_id, |
|
|
|
}) |
|
|
|
}) |
|
|
|
).getColOptions<LinkToAnotherRecordType>(); |
|
|
|
).getColOptions<LinkToAnotherRecordType>(); |
|
|
|
|
|
|
|
|
|
|
@ -191,28 +162,23 @@ export async function columnAdd( |
|
|
|
const relatedTable = await relatedColumn.getModel(); |
|
|
|
const relatedTable = await relatedColumn.getModel(); |
|
|
|
if ( |
|
|
|
if ( |
|
|
|
!(await relatedTable.getColumns()).find( |
|
|
|
!(await relatedTable.getColumns()).find( |
|
|
|
(c) => |
|
|
|
(c) => c.id === (payload as RollupColumnReqType).fk_rollup_column_id |
|
|
|
c.id === (req.body as RollupColumnReqType).fk_rollup_column_id |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
throw new Error('Rollup column not found in related table'); |
|
|
|
throw new Error('Rollup column not found in related table'); |
|
|
|
|
|
|
|
|
|
|
|
await Column.insert({ |
|
|
|
|
|
|
|
...colBody, |
|
|
|
|
|
|
|
fk_model_id: table.id, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case UITypes.Lookup: |
|
|
|
async function validateLookupPayload( |
|
|
|
{ |
|
|
|
payload: ColumnReqType & { uidt: UITypes } |
|
|
|
|
|
|
|
) { |
|
|
|
validateParams( |
|
|
|
validateParams( |
|
|
|
['title', 'fk_relation_column_id', 'fk_lookup_column_id'], |
|
|
|
['title', 'fk_relation_column_id', 'fk_lookup_column_id'], |
|
|
|
req.body |
|
|
|
payload |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const relation = await ( |
|
|
|
const relation = await ( |
|
|
|
await Column.get({ |
|
|
|
await Column.get({ |
|
|
|
colId: (req.body as LookupColumnReqType).fk_relation_column_id, |
|
|
|
colId: (payload as LookupColumnReqType).fk_relation_column_id, |
|
|
|
}) |
|
|
|
}) |
|
|
|
).getColOptions<LinkToAnotherRecordType>(); |
|
|
|
).getColOptions<LinkToAnotherRecordType>(); |
|
|
|
|
|
|
|
|
|
|
@ -238,11 +204,55 @@ export async function columnAdd( |
|
|
|
const relatedTable = await relatedColumn.getModel(); |
|
|
|
const relatedTable = await relatedColumn.getModel(); |
|
|
|
if ( |
|
|
|
if ( |
|
|
|
!(await relatedTable.getColumns()).find( |
|
|
|
!(await relatedTable.getColumns()).find( |
|
|
|
(c) => |
|
|
|
(c) => c.id === (payload as LookupColumnReqType).fk_lookup_column_id |
|
|
|
c.id === (req.body as LookupColumnReqType).fk_lookup_column_id |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
throw new Error('Lookup column not found in related table'); |
|
|
|
throw new Error('Lookup column not found in related table'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function columnAdd( |
|
|
|
|
|
|
|
req: Request<any, any, ColumnReqType & { uidt: UITypes }>, |
|
|
|
|
|
|
|
res: Response<TableType> |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
const table = await Model.getWithInfo({ |
|
|
|
|
|
|
|
id: req.params.tableId, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
const base = await Base.get(table.base_id); |
|
|
|
|
|
|
|
const project = await base.getProject(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
!isVirtualCol(req.body) && |
|
|
|
|
|
|
|
!(await Column.checkTitleAvailable({ |
|
|
|
|
|
|
|
column_name: req.body.column_name, |
|
|
|
|
|
|
|
fk_model_id: req.params.tableId, |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
NcError.badRequest('Duplicate column name'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
!(await Column.checkAliasAvailable({ |
|
|
|
|
|
|
|
title: req.body.title || req.body.column_name, |
|
|
|
|
|
|
|
fk_model_id: req.params.tableId, |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
NcError.badRequest('Duplicate column alias'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let colBody: any = req.body; |
|
|
|
|
|
|
|
switch (colBody.uidt) { |
|
|
|
|
|
|
|
case UITypes.Rollup: |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
await validateRollupPayload(req.body); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Column.insert({ |
|
|
|
|
|
|
|
...colBody, |
|
|
|
|
|
|
|
fk_model_id: table.id, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case UITypes.Lookup: |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
await validateLookupPayload(req.body); |
|
|
|
|
|
|
|
|
|
|
|
await Column.insert({ |
|
|
|
await Column.insert({ |
|
|
|
...colBody, |
|
|
|
...colBody, |
|
|
@ -753,6 +763,30 @@ export async function columnSetAsPrimary(req: Request, res: Response) { |
|
|
|
res.json(await Model.updatePrimaryColumn(column.fk_model_id, column.id)); |
|
|
|
res.json(await Model.updatePrimaryColumn(column.fk_model_id, column.id)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isAllPropsPresent = (obj: Record<string, any>, props: string[]) => { |
|
|
|
|
|
|
|
return props.every((prop) => obj[prop]); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function updateRollupOrLookup(colBody: any, column: Column<any>) { |
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
UITypes.Lookup === column.uidt && |
|
|
|
|
|
|
|
isAllPropsPresent(colBody, ['fk_lookup_column_id', 'fk_relation_column_id']) |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
await validateLookupPayload(colBody); |
|
|
|
|
|
|
|
await Column.update(column.id, colBody); |
|
|
|
|
|
|
|
} else if ( |
|
|
|
|
|
|
|
UITypes.Rollup === column.uidt && |
|
|
|
|
|
|
|
isAllPropsPresent(colBody, [ |
|
|
|
|
|
|
|
'fk_relation_column_id', |
|
|
|
|
|
|
|
'fk_rollup_column_id', |
|
|
|
|
|
|
|
'rollup_function', |
|
|
|
|
|
|
|
]) |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
await validateRollupPayload(colBody); |
|
|
|
|
|
|
|
await Column.update(column.id, colBody); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function columnUpdate(req: Request, res: Response<TableType>) { |
|
|
|
export async function columnUpdate(req: Request, res: Response<TableType>) { |
|
|
|
const column = await Column.get({ colId: req.params.columnId }); |
|
|
|
const column = await Column.get({ colId: req.params.columnId }); |
|
|
|
|
|
|
|
|
|
|
@ -824,6 +858,7 @@ export async function columnUpdate(req: Request, res: Response<TableType>) { |
|
|
|
title: colBody.title, |
|
|
|
title: colBody.title, |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
await updateRollupOrLookup(colBody, column); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
NcError.notImplemented( |
|
|
|
NcError.notImplemented( |
|
|
|
`Updating ${colBody.uidt} => ${colBody.uidt} is not implemented` |
|
|
|
`Updating ${colBody.uidt} => ${colBody.uidt} is not implemented` |
|
|
|