WIP - add owner to contact
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
exports.up = async function (knex) {
|
||||||
|
// Add ownerId column to contacts
|
||||||
|
await knex.schema.alterTable('contacts', (table) => {
|
||||||
|
table.uuid('ownerId');
|
||||||
|
table
|
||||||
|
.foreign('ownerId')
|
||||||
|
.references('id')
|
||||||
|
.inTable('users')
|
||||||
|
.onDelete('SET NULL');
|
||||||
|
table.index(['ownerId']);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add ownerId field definition metadata for Contact object
|
||||||
|
const contactObject = await knex('object_definitions')
|
||||||
|
.where('apiName', 'Contact')
|
||||||
|
.first();
|
||||||
|
|
||||||
|
if (contactObject) {
|
||||||
|
const existingField = await knex('field_definitions')
|
||||||
|
.where({
|
||||||
|
objectDefinitionId: contactObject.id,
|
||||||
|
apiName: 'ownerId',
|
||||||
|
})
|
||||||
|
.first();
|
||||||
|
|
||||||
|
if (!existingField) {
|
||||||
|
await knex('field_definitions').insert({
|
||||||
|
id: knex.raw('(UUID())'),
|
||||||
|
objectDefinitionId: contactObject.id,
|
||||||
|
apiName: 'ownerId',
|
||||||
|
label: 'Owner',
|
||||||
|
type: 'Reference',
|
||||||
|
referenceObject: 'User',
|
||||||
|
isSystem: true,
|
||||||
|
isCustom: false,
|
||||||
|
displayOrder: 4,
|
||||||
|
created_at: knex.fn.now(),
|
||||||
|
updated_at: knex.fn.now(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.down = async function (knex) {
|
||||||
|
const contactObject = await knex('object_definitions')
|
||||||
|
.where('apiName', 'Contact')
|
||||||
|
.first();
|
||||||
|
|
||||||
|
if (contactObject) {
|
||||||
|
await knex('field_definitions')
|
||||||
|
.where({
|
||||||
|
objectDefinitionId: contactObject.id,
|
||||||
|
apiName: 'ownerId',
|
||||||
|
})
|
||||||
|
.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
await knex.schema.alterTable('contacts', (table) => {
|
||||||
|
table.dropForeign(['ownerId']);
|
||||||
|
table.dropColumn('ownerId');
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -7,6 +7,7 @@ export class Contact extends BaseModel {
|
|||||||
firstName!: string;
|
firstName!: string;
|
||||||
lastName!: string;
|
lastName!: string;
|
||||||
accountId!: string;
|
accountId!: string;
|
||||||
|
ownerId?: string;
|
||||||
|
|
||||||
static relationMappings = {
|
static relationMappings = {
|
||||||
account: {
|
account: {
|
||||||
@@ -17,5 +18,13 @@ export class Contact extends BaseModel {
|
|||||||
to: 'accounts.id',
|
to: 'accounts.id',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
owner: {
|
||||||
|
relation: BaseModel.BelongsToOneRelation,
|
||||||
|
modelClass: 'user.model',
|
||||||
|
join: {
|
||||||
|
from: 'contacts.ownerId',
|
||||||
|
to: 'users.id',
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user