Skip to content

Commit

Permalink
refactor: rename owner to createdBy
Browse files Browse the repository at this point in the history
  • Loading branch information
marrouchi committed Jan 15, 2025
1 parent 01ec94e commit ed39571
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 79 deletions.
8 changes: 4 additions & 4 deletions api/src/attachment/controllers/attachment.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ describe('AttachmentController', () => {
name: attachmentFile.originalname,
location: expect.stringMatching(new RegExp(`^/${name}`)),
context: 'block_attachment',
ownerType: 'User',
owner: '9'.repeat(24),
createdByRef: 'User',
createdBy: '9'.repeat(24),
});
expect(result).toEqualPayload(
[
{
...attachment,
context: 'block_attachment',
ownerType: 'User',
owner: '9'.repeat(24),
createdByRef: 'User',
createdBy: '9'.repeat(24),
},
],
[...IGNORED_TEST_FIELDS, 'location', 'url'],
Expand Down
4 changes: 2 additions & 2 deletions api/src/attachment/controllers/attachment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ export class AttachmentController extends BaseController<Attachment> {
size: file.size,
type: file.mimetype,
context,
owner: userId,
ownerType: 'User',
createdBy: userId,
createdByRef: 'User',
});
attachments.push(attachment);
}
Expand Down
12 changes: 6 additions & 6 deletions api/src/attachment/dto/attachment.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import { IsObjectId } from '@/utils/validation-rules/is-object-id';

import {
AttachmentContext,
AttachmentOwnerType,
AttachmentCreatedByRef,
TAttachmentContext,
TAttachmentOwnerType,
TAttachmentCreatedByRef,
} from '../types';

export class AttachmentMetadataDto {
Expand Down Expand Up @@ -81,12 +81,12 @@ export class AttachmentMetadataDto {
*/
@ApiPropertyOptional({
description: 'Attachment Owner Type',
enum: Object.values(AttachmentOwnerType),
enum: Object.values(AttachmentCreatedByRef),
})
@IsString()
@IsNotEmpty()
@IsIn(Object.values(AttachmentOwnerType))
ownerType: TAttachmentOwnerType;
@IsIn(Object.values(AttachmentCreatedByRef))
createdByRef: TAttachmentCreatedByRef;

/**
* Attachment Owner : Subscriber or User ID
Expand All @@ -98,7 +98,7 @@ export class AttachmentMetadataDto {
@IsString()
@IsNotEmpty()
@IsObjectId({ message: 'Owner must be a valid ObjectId' })
owner: string;
createdBy: string;
}

export class AttachmentCreateDto extends AttachmentMetadataDto {
Expand Down
12 changes: 6 additions & 6 deletions api/src/attachment/mocks/attachment.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export const attachment: Attachment = {
id: '65940d115178607da65c82b6',
createdAt: new Date(),
updatedAt: new Date(),
owner: '1',
ownerType: 'User',
createdBy: '1',
createdByRef: 'User',
};

export const attachmentFile: Express.Multer.File = {
Expand Down Expand Up @@ -50,8 +50,8 @@ export const attachments: Attachment[] = [
id: '65940d115178607da65c82b7',
createdAt: new Date(),
updatedAt: new Date(),
owner: '1',
ownerType: 'User',
createdBy: '1',
createdByRef: 'User',
},
{
name: 'Screenshot from 2022-03-18 08-58-15-af61e7f71281f9fd3f1ad7ad10107741c.png',
Expand All @@ -64,7 +64,7 @@ export const attachments: Attachment[] = [
id: '65940d115178607da65c82b8',
createdAt: new Date(),
updatedAt: new Date(),
owner: '1',
ownerType: 'User',
createdBy: '1',
createdByRef: 'User',
},
];
26 changes: 13 additions & 13 deletions api/src/attachment/schemas/attachment.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import {

import {
AttachmentContext,
AttachmentOwnerType,
AttachmentCreatedByRef,
TAttachmentContext,
TAttachmentOwnerType,
TAttachmentCreatedByRef,
} from '../types';
import { MIME_REGEX } from '../utilities';

Expand Down Expand Up @@ -79,20 +79,20 @@ export class AttachmentStub extends BaseSchema {
channel?: Partial<Record<ChannelName, any>>;

/**
* Object ID of the owner (depending on the owner type)
* Object ID of the createdBy (depending on the createdBy type)
*/
@Prop({
type: MongooseSchema.Types.ObjectId,
refPath: 'ownerType',
refPath: 'createdByRef',
default: null,
})
owner: unknown;
createdBy: unknown;

/**
* Type of the owner (depending on the owner type)
* Type of the createdBy (depending on the createdBy type)
*/
@Prop({ type: String, enum: Object.values(AttachmentOwnerType) })
ownerType: TAttachmentOwnerType;
@Prop({ type: String, enum: Object.values(AttachmentCreatedByRef) })
createdByRef: TAttachmentCreatedByRef;

/**
* Context of the attachment
Expand Down Expand Up @@ -142,20 +142,20 @@ export class AttachmentStub extends BaseSchema {

@Schema({ timestamps: true })
export class Attachment extends AttachmentStub {
@Transform(({ obj }) => obj.owner?.toString() || null)
owner: string | null;
@Transform(({ obj }) => obj.createdBy?.toString() || null)
createdBy: string | null;
}

@Schema({ timestamps: true })
export class UserAttachmentFull extends AttachmentStub {
@Type(() => User)
owner: User | undefined;
createdBy: User | undefined;
}

@Schema({ timestamps: true })
export class SubscriberAttachmentFull extends AttachmentStub {
@Type(() => Subscriber)
owner: Subscriber | undefined;
createdBy: Subscriber | undefined;
}

export type AttachmentDocument = THydratedDocument<Attachment>;
Expand All @@ -182,4 +182,4 @@ export type AttachmentPopulate = keyof TFilterPopulateFields<
AttachmentStub
>;

export const ATTACHMENT_POPULATE: AttachmentPopulate[] = ['owner'];
export const ATTACHMENT_POPULATE: AttachmentPopulate[] = ['createdBy'];
6 changes: 3 additions & 3 deletions api/src/attachment/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import { Readable, Stream } from 'stream';

/**
* Defines the types of owners for an attachment,
* Defines the types of createdBys for an attachment,
* indicating whether the file belongs to a User or a Subscriber.
*/
export enum AttachmentOwnerType {
export enum AttachmentCreatedByRef {
User = 'User',
Subscriber = 'Subscriber',
}

export type TAttachmentOwnerType = `${AttachmentOwnerType}`;
export type TAttachmentCreatedByRef = `${AttachmentCreatedByRef}`;

/**
* Defines the various contexts in which an attachment can exist.
Expand Down
4 changes: 2 additions & 2 deletions api/src/channel/lib/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ export default abstract class ChannelHandler<
type,
size,
context: 'message_attachment',
ownerType: 'Subscriber',
owner: subscriber.id,
createdByRef: 'Subscriber',
createdBy: subscriber.id,
});
}),
);
Expand Down
4 changes: 2 additions & 2 deletions api/src/channel/lib/__test__/common.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ const attachment: Attachment = {
},
},
context: 'block_attachment',
ownerType: 'User',
owner: null,
createdByRef: 'User',
createdBy: null,
createdAt: new Date(),
updatedAt: new Date(),
};
Expand Down
4 changes: 2 additions & 2 deletions api/src/chat/services/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ export class ChatService {
size,
type,
context: 'subscriber_avatar',
ownerType: 'Subscriber',
owner: subscriber.id,
createdByRef: 'Subscriber',
createdBy: subscriber.id,
});

if (avatar) {
Expand Down
8 changes: 4 additions & 4 deletions api/src/extensions/channels/web/base-web-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,8 @@ export default abstract class BaseWebChannelHandler<
size: Buffer.byteLength(data.file),
type: data.type,
context: 'message_attachment',
ownerType: 'Subscriber',
owner: req.session.web.profile?.id,
createdByRef: 'Subscriber',
createdBy: req.session.web.profile?.id,
});
} catch (err) {
this.logger.error(
Expand Down Expand Up @@ -682,8 +682,8 @@ export default abstract class BaseWebChannelHandler<
size: file.size,
type: file.mimetype,
context: 'message_attachment',
ownerType: 'Subscriber',
owner: req.session.web.profile?.id,
createdByRef: 'Subscriber',
createdBy: req.session.web.profile?.id,
});
} catch (err) {
this.logger.error(
Expand Down
36 changes: 18 additions & 18 deletions api/src/migration/migrations/1735836154221-v-2-2-0.migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { v4 as uuidv4 } from 'uuid';
import attachmentSchema, {
Attachment,
} from '@/attachment/schemas/attachment.schema';
import { AttachmentContext, AttachmentOwnerType } from '@/attachment/types';
import { AttachmentContext, AttachmentCreatedByRef } from '@/attachment/types';
import blockSchema, { Block } from '@/chat/schemas/block.schema';
import messageSchema, { Message } from '@/chat/schemas/message.schema';
import subscriberSchema, { Subscriber } from '@/chat/schemas/subscriber.schema';
Expand Down Expand Up @@ -80,8 +80,8 @@ const populateBlockAttachments = async ({ logger }: MigrationServices) => {
{
$set: {
context: AttachmentContext.BlockAttachment,
ownerType: AttachmentOwnerType.User,
owner: user._id,
createdByRef: AttachmentCreatedByRef.User,
createdBy: user._id,
},
},
);
Expand All @@ -102,7 +102,7 @@ const populateBlockAttachments = async ({ logger }: MigrationServices) => {
};

/**
* Updates setting attachment documents to populate new attributes (context, owner, ownerType)
* Updates setting attachment documents to populate new attributes (context, createdBy, createdByRef)
*
* @returns Resolves when the migration process is complete.
*/
Expand Down Expand Up @@ -130,8 +130,8 @@ const populateSettingAttachments = async ({ logger }: MigrationServices) => {
{
$set: {
context: AttachmentContext.SettingAttachment,
ownerType: AttachmentOwnerType.User,
owner: user._id,
createdByRef: AttachmentCreatedByRef.User,
createdBy: user._id,
},
},
);
Expand All @@ -146,7 +146,7 @@ const populateSettingAttachments = async ({ logger }: MigrationServices) => {
};

/**
* Updates user attachment documents to populate new attributes (context, owner, ownerType)
* Updates user attachment documents to populate new attributes (context, createdBy, createdByRef)
*
* @returns Resolves when the migration process is complete.
*/
Expand All @@ -168,8 +168,8 @@ const populateUserAvatars = async ({ logger }: MigrationServices) => {
{
$set: {
context: AttachmentContext.UserAvatar,
ownerType: AttachmentOwnerType.User,
owner: user._id,
createdByRef: AttachmentCreatedByRef.User,
createdBy: user._id,
},
},
);
Expand All @@ -184,7 +184,7 @@ const populateUserAvatars = async ({ logger }: MigrationServices) => {

/**
* Updates subscriber documents with their corresponding avatar attachments,
* populate new attributes (context, owner, ownerType) and moves avatar files to a new directory.
* populate new attributes (context, createdBy, createdByRef) and moves avatar files to a new directory.
*
* @returns Resolves when the migration process is complete.
*/
Expand Down Expand Up @@ -229,8 +229,8 @@ const populateSubscriberAvatars = async ({ logger }: MigrationServices) => {
{
$set: {
context: AttachmentContext.SubscriberAvatar,
ownerType: AttachmentOwnerType.Subscriber,
owner: subscriber._id,
createdByRef: AttachmentCreatedByRef.Subscriber,
createdBy: subscriber._id,
},
},
);
Expand Down Expand Up @@ -361,8 +361,8 @@ const undoPopulateAttachments = async ({ logger }: MigrationServices) => {
{
$unset: {
context: '',
ownerType: '',
owner: '',
createdByRef: '',
createdBy: '',
},
},
);
Expand Down Expand Up @@ -640,8 +640,8 @@ const migrateAndPopulateAttachmentMessages = async ({
await attachmentService.updateOne(
msg.message.attachment.payload.attachment_id,
{
ownerType: msg.sender ? 'Subscriber' : 'User',
owner: msg.sender ? msg.sender : adminUser.id,
createdByRef: msg.sender ? 'Subscriber' : 'User',
createdBy: msg.sender ? msg.sender : adminUser.id,
context: 'message_attachment',
},
);
Expand Down Expand Up @@ -672,8 +672,8 @@ const migrateAndPopulateAttachmentMessages = async ({
size: fileBuffer.length,
type: response.headers['content-type'],
channel: {},
owner: msg.sender ? msg.sender : adminUser.id,
ownerType: msg.sender ? 'Subscriber' : 'User',
createdBy: msg.sender ? msg.sender : adminUser.id,
createdByRef: msg.sender ? 'Subscriber' : 'User',
context: 'message_attachment',
});
await updateAttachmentId(msg._id, attachment.id);
Expand Down
4 changes: 2 additions & 2 deletions api/src/user/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ export class ReadWriteUserController extends ReadOnlyUserController {
size: avatarFile.size,
type: avatarFile.mimetype,
context: 'user_avatar',
ownerType: 'User',
owner: req.user.id,
createdByRef: 'User',
createdBy: req.user.id,
})
: undefined;

Expand Down
4 changes: 2 additions & 2 deletions api/src/user/types/index.type.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/

export type TRelation = 'role' | 'owner';
export type TRelation = 'role' | 'createdBy';
Loading

0 comments on commit ed39571

Please sign in to comment.