|
|
|
@ -15,9 +15,13 @@ import {
|
|
|
|
|
import { |
|
|
|
|
AuditOperationSubTypes, |
|
|
|
|
AuditOperationTypes, |
|
|
|
|
ColumnReqType, |
|
|
|
|
isVirtualCol, |
|
|
|
|
LinkToAnotherColumnReqType, |
|
|
|
|
LinkToAnotherRecordType, |
|
|
|
|
LookupColumnReqType, |
|
|
|
|
RelationTypes, |
|
|
|
|
RollupColumnReqType, |
|
|
|
|
substituteColumnAliasWithIdInFormula, |
|
|
|
|
substituteColumnIdWithAliasInFormula, |
|
|
|
|
TableType, |
|
|
|
@ -96,7 +100,10 @@ async function createHmAndBtColumn(
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export async function columnAdd(req: Request, res: Response<TableType>) { |
|
|
|
|
export async function columnAdd( |
|
|
|
|
req: Request<any, any, ColumnReqType & { uidt: UITypes }>, |
|
|
|
|
res: Response<TableType> |
|
|
|
|
) { |
|
|
|
|
const table = await Model.getWithInfo({ |
|
|
|
|
id: req.params.tableId, |
|
|
|
|
}); |
|
|
|
@ -121,7 +128,7 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
|
|
|
|
|
NcError.badRequest('Duplicate column alias'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let colBody = req.body; |
|
|
|
|
let colBody: any = req.body; |
|
|
|
|
switch (colBody.uidt) { |
|
|
|
|
case UITypes.Rollup: |
|
|
|
|
{ |
|
|
|
@ -137,7 +144,7 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
|
|
|
|
|
|
|
|
|
|
const relation = await ( |
|
|
|
|
await Column.get({ |
|
|
|
|
colId: req.body.fk_relation_column_id, |
|
|
|
|
colId: (req.body as RollupColumnReqType).fk_relation_column_id, |
|
|
|
|
}) |
|
|
|
|
).getColOptions<LinkToAnotherRecordType>(); |
|
|
|
|
|
|
|
|
@ -163,7 +170,8 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
|
|
|
|
|
const relatedTable = await relatedColumn.getModel(); |
|
|
|
|
if ( |
|
|
|
|
!(await relatedTable.getColumns()).find( |
|
|
|
|
(c) => c.id === req.body.fk_rollup_column_id |
|
|
|
|
(c) => |
|
|
|
|
c.id === (req.body as RollupColumnReqType).fk_rollup_column_id |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
throw new Error('Rollup column not found in related table'); |
|
|
|
@ -183,7 +191,7 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
|
|
|
|
|
|
|
|
|
|
const relation = await ( |
|
|
|
|
await Column.get({ |
|
|
|
|
colId: req.body.fk_relation_column_id, |
|
|
|
|
colId: (req.body as LookupColumnReqType).fk_relation_column_id, |
|
|
|
|
}) |
|
|
|
|
).getColOptions<LinkToAnotherRecordType>(); |
|
|
|
|
|
|
|
|
@ -209,7 +217,8 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
|
|
|
|
|
const relatedTable = await relatedColumn.getModel(); |
|
|
|
|
if ( |
|
|
|
|
!(await relatedTable.getColumns()).find( |
|
|
|
|
(c) => c.id === req.body.fk_lookup_column_id |
|
|
|
|
(c) => |
|
|
|
|
c.id === (req.body as LookupColumnReqType).fk_lookup_column_id |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
throw new Error('Lookup column not found in related table'); |
|
|
|
@ -227,14 +236,21 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
|
|
|
|
|
validateParams(['parentId', 'childId', 'type'], req.body); |
|
|
|
|
|
|
|
|
|
// get parent and child models
|
|
|
|
|
const parent = await Model.getWithInfo({ id: req.body.parentId }); |
|
|
|
|
const child = await Model.getWithInfo({ id: req.body.childId }); |
|
|
|
|
const parent = await Model.getWithInfo({ |
|
|
|
|
id: (req.body as LinkToAnotherColumnReqType).parentId, |
|
|
|
|
}); |
|
|
|
|
const child = await Model.getWithInfo({ |
|
|
|
|
id: (req.body as LinkToAnotherColumnReqType).childId, |
|
|
|
|
}); |
|
|
|
|
let childColumn: Column; |
|
|
|
|
|
|
|
|
|
const sqlMgr = await ProjectMgrv2.getSqlMgr({ |
|
|
|
|
id: base.project_id, |
|
|
|
|
}); |
|
|
|
|
if (req.body.type === 'hm' || req.body.type === 'bt') { |
|
|
|
|
if ( |
|
|
|
|
(req.body as LinkToAnotherColumnReqType).type === 'hm' || |
|
|
|
|
(req.body as LinkToAnotherColumnReqType).type === 'bt' |
|
|
|
|
) { |
|
|
|
|
// populate fk column name
|
|
|
|
|
const fkColName = getUniqueColumnName( |
|
|
|
|
await child.getColumns(), |
|
|
|
@ -285,7 +301,7 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
|
|
|
|
|
childColumn = await Column.get({ colId: id }); |
|
|
|
|
|
|
|
|
|
// ignore relation creation if virtual
|
|
|
|
|
if (!req.body.virtual) { |
|
|
|
|
if (!(req.body as LinkToAnotherColumnReqType).virtual) { |
|
|
|
|
// create relation
|
|
|
|
|
await sqlMgr.sqlOpPlus(base, 'relationCreate', { |
|
|
|
|
childColumn: fkColName, |
|
|
|
@ -315,11 +331,11 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
|
|
|
|
|
child, |
|
|
|
|
parent, |
|
|
|
|
childColumn, |
|
|
|
|
req.body.type, |
|
|
|
|
req.body.title, |
|
|
|
|
req.body.virtual |
|
|
|
|
(req.body as LinkToAnotherColumnReqType).type as RelationTypes, |
|
|
|
|
(req.body as LinkToAnotherColumnReqType).title, |
|
|
|
|
(req.body as LinkToAnotherColumnReqType).virtual |
|
|
|
|
); |
|
|
|
|
} else if (req.body.type === 'mm') { |
|
|
|
|
} else if ((req.body as LinkToAnotherColumnReqType).type === 'mm') { |
|
|
|
|
const aTn = `${project?.prefix ?? ''}_nc_m2m_${randomID()}`; |
|
|
|
|
const aTnAlias = aTn; |
|
|
|
|
|
|
|
|
@ -378,7 +394,7 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
|
|
|
|
|
columns: associateTableCols, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!req.body.virtual) { |
|
|
|
|
if (!(req.body as LinkToAnotherColumnReqType).virtual) { |
|
|
|
|
const rel1Args = { |
|
|
|
|
...req.body, |
|
|
|
|
childTable: aTn, |
|
|
|
@ -412,7 +428,7 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
|
|
|
|
|
childCol, |
|
|
|
|
null, |
|
|
|
|
null, |
|
|
|
|
req.body.virtual, |
|
|
|
|
(req.body as LinkToAnotherColumnReqType).virtual, |
|
|
|
|
true |
|
|
|
|
); |
|
|
|
|
await createHmAndBtColumn( |
|
|
|
@ -421,7 +437,7 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
|
|
|
|
|
parentCol, |
|
|
|
|
null, |
|
|
|
|
null, |
|
|
|
|
req.body.virtual, |
|
|
|
|
(req.body as LinkToAnotherColumnReqType).virtual, |
|
|
|
|
true |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|