Skip to content

Commit

Permalink
Document User, Profile, Role, and Permission; update naming conventio…
Browse files Browse the repository at this point in the history
…n from IEntity to EntityParamaters; update JWT token type
  • Loading branch information
DylanBulmer committed Jul 26, 2024
1 parent e236740 commit 861bb98
Show file tree
Hide file tree
Showing 19 changed files with 317 additions and 183 deletions.
5 changes: 3 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "@codr/models",
"version": "0.1.5",
"version": "0.1.6",
"exports": "./mod.ts",
"fmt": {
"lineWidth": 80,
"indentWidth": 2,
"useTabs": false,
"semiColons": true,
"singleQuote": false
"singleQuote": false,
"proseWrap": "always"
},
"publish": {
"exclude": [".github/*", "tests/*.test.ts", ".gitignore", "coverage/*"]
Expand Down
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// export * as Types from "./types/mod.ts";
export * from "./models/mod.ts";
export type { JwtPayload } from "./types/mod.ts";
8 changes: 4 additions & 4 deletions src/models/Annotation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ObjectId } from "bson";
import { Base, type IBase } from "./Base.ts";
import { Base, type BaseParameters } from "./Base.ts";
import type { AtLeast } from "../types/mod.ts";

export interface IAnnotation extends IBase<"Annotation"> {
export interface AnnotationParameters extends BaseParameters<"Annotation"> {
projectId: ObjectId;
datasetId: ObjectId;
sampleId: ObjectId;
Expand Down Expand Up @@ -30,7 +30,7 @@ export class Annotation extends Base<"Annotation"> {
createdBy,
updatedBy,
}: AtLeast<
IAnnotation,
AnnotationParameters,
| "createdBy"
| "projectId"
| "datasetId"
Expand All @@ -46,7 +46,7 @@ export class Annotation extends Base<"Annotation"> {
this.annotatedBy = annotatedBy;
}

toJSON(): Omit<IAnnotation, "kind"> {
toJSON(): Omit<AnnotationParameters, "kind"> {
const json = super.toJSON();
return {
...json,
Expand Down
5 changes: 3 additions & 2 deletions src/models/Audit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { ObjectId } from "bson";
import type { IBase } from "./Base.ts";
import type { BaseParameters } from "./Base.ts";
import type { ActionCode, ResourceCode } from "../types/mod.ts";

export interface IAudit<T> extends Omit<IBase<"Audit">, "createdBy"> {
export interface AuditParameters<T>
extends Omit<BaseParameters<"Audit">, "createdBy"> {
type: ResourceCode; // Where: what system processed this event.
action: ActionCode; // How: what action was taken.
userId: ObjectId; // Who: which user made the change.
Expand Down
43 changes: 0 additions & 43 deletions src/models/Authorization.ts

This file was deleted.

18 changes: 9 additions & 9 deletions src/models/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { AtLeast } from "../types/mod.ts";
/**
* Base entity parameters for all database entities.
*/
export interface IBase<Kind extends string> {
export interface BaseParameters<Kind extends string> {
/** Typescript annotation for permissioning/authorization purposes. */
readonly kind: Kind;
/** Entity version stored in the database. */
Expand All @@ -22,12 +22,12 @@ export interface IBase<Kind extends string> {
}

export class Base<K extends string> {
readonly _version: IBase<K>["_version"];
readonly _id: IBase<K>["_id"];
readonly createdAt: IBase<K>["createdAt"];
readonly updatedAt: IBase<K>["updatedAt"];
readonly createdBy: IBase<K>["createdBy"];
readonly updatedBy: IBase<K>["updatedBy"];
readonly _version: BaseParameters<K>["_version"];
readonly _id: BaseParameters<K>["_id"];
readonly createdAt: BaseParameters<K>["createdAt"];
readonly updatedAt: BaseParameters<K>["updatedAt"];
readonly createdBy: BaseParameters<K>["createdBy"];
readonly updatedBy: BaseParameters<K>["updatedBy"];

constructor({
createdAt,
Expand All @@ -36,7 +36,7 @@ export class Base<K extends string> {
updatedBy,
_id,
_version = 0,
}: AtLeast<IBase<K>, "createdBy">) {
}: AtLeast<BaseParameters<K>, "createdBy">) {
this._version = _version;
this._id = _id || new ObjectId();

Expand All @@ -52,7 +52,7 @@ export class Base<K extends string> {
* Transforms the base class object into a json object. Useful for saving the entity to the database.
* @returns a json representation of the base entity.
*/
toJSON(): Omit<IBase<K>, "kind"> {
toJSON(): Omit<BaseParameters<K>, "kind"> {
return {
_version: this._version,
_id: this._id,
Expand Down
8 changes: 4 additions & 4 deletions src/models/Dataset.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ObjectId } from "bson";
import type { AtLeast, Flags } from "../types/mod.ts";
import { Base, type IBase } from "./Base.ts";
import { Base, type BaseParameters } from "./Base.ts";

export interface IDataset extends IBase<"Dataset"> {
export interface DatasetParameters extends BaseParameters<"Dataset"> {
projectId: ObjectId;
flags: Flags;
name: string;
Expand All @@ -23,7 +23,7 @@ export class Dataset extends Base<"Dataset"> {
name,
createdBy,
updatedBy,
}: AtLeast<IDataset, "createdBy" | "name" | "flags" | "projectId">) {
}: AtLeast<DatasetParameters, "createdBy" | "name" | "flags" | "projectId">) {
super({
_id,
_version,
Expand All @@ -37,7 +37,7 @@ export class Dataset extends Base<"Dataset"> {
this.name = name;
}

toJSON(): Omit<IDataset, "kind"> {
toJSON(): Omit<DatasetParameters, "kind"> {
const json = super.toJSON();
return {
...json,
Expand Down
54 changes: 42 additions & 12 deletions src/models/Group.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
import type { ObjectId } from "bson";
import { Base, type IBase } from "./Base.ts";
import { Base, type BaseParameters } from "./Base.ts";
import type { AtLeast, Flags } from "../types/mod.ts";

export interface IGroup<
/**
* Parameters for creating a {@link Group} entity.
*/
export interface GroupParameters<
Kind extends string = "Group",
F extends object = Flags,
> extends IBase<Kind> {
members: IGroupMember[];
> extends BaseParameters<Kind> {
name: string;
members: GroupMemberParameters[];
flags: F;
}

export interface IGroupMember {
type: GroupMemberEnum;
/**
* Parameters for adding Group members to the {@link GroupParameters}.
*/
export interface GroupMemberParameters {
type: GroupMemberType;
_id: ObjectId;
}

export enum GroupMemberEnum {
"USER" = "USER",
"TEAM" = "TEAM",
/**
* Type codes for the {@link GroupMemberParameters} object.
*/
export enum GroupMemberType {
"User" = "USER",
"Group" = "GROUP",
}

export class Group<
K extends string = "Group",
F extends object = Flags,
> extends Base<K> {
members: IGroupMember[];
members: GroupMemberParameters[];
name: string;
flags: F;

Expand All @@ -39,14 +48,35 @@ export class Group<
updatedAt,
createdBy,
updatedBy,
}: AtLeast<IGroup<K, F>, "createdBy" | "name" | "members" | "flags">) {
}: AtLeast<
GroupParameters<K, F>,
"createdBy" | "name" | "members" | "flags"
>) {
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
this.name = name;
this.members = members;
this.flags = flags;
}

toJSON(): Omit<IGroup<K, F>, "kind"> {
addUser(id: ObjectId): GroupMemberParameters {
const member = {
type: GroupMemberType.User,
_id: id,
};
this.members.push(member);
return member;
}

addGroup(id: ObjectId): GroupMemberParameters {
const member = {
type: GroupMemberType.Group,
_id: id,
};
this.members.push(member);
return member;
}

toJSON(): Omit<GroupParameters<K, F>, "kind"> {
const json = super.toJSON();
return {
...json,
Expand Down
11 changes: 7 additions & 4 deletions src/models/Message.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ObjectId } from "bson";
import type { AtLeast, MessageType } from "../types/mod.ts";
import { Base, type IBase } from "./Base.ts";
import { Base, type BaseParameters } from "./Base.ts";

export interface IMessage extends IBase<"Message"> {
export interface MessageParameters extends BaseParameters<"Message"> {
type: MessageType;
subject: string;
body: string;
Expand All @@ -26,15 +26,18 @@ export class Message extends Base<"Message"> {
updatedAt,
createdBy,
updatedBy,
}: AtLeast<IMessage, "createdBy" | "body" | "subject" | "to" | "type">) {
}: AtLeast<
MessageParameters,
"createdBy" | "body" | "subject" | "to" | "type"
>) {
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
this.body = body;
this.subject = subject;
this.to = to;
this.type = type;
}

toJSON(): Omit<IMessage, "kind"> {
toJSON(): Omit<MessageParameters, "kind"> {
const json = super.toJSON();
return {
...json,
Expand Down
42 changes: 20 additions & 22 deletions src/models/Organization.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import type { AtLeast } from "../types/mod.ts";
import { Base, type IBase } from "./Base.ts";

/**
* Flags used in the {@link IOrganization} interface.
*/
interface OrganizationFlags {
/** Whether or not the organization is active */
isActive: boolean;
/** Whether or not the organization is soft deleted */
isDeleted: boolean;
/** Whether or not the organization is a demo organization */
isDemo: boolean;
}
import { Base, type BaseParameters } from "./Base.ts";

/**
* Parameters for building an {@link Organization} entity.
*/
export interface IOrganization extends IBase<"Organization"> {
export interface OrganizationParameters extends BaseParameters<"Organization"> {
/** A list of domains (and ports) linked to the Organziation */
domains: string[];
/** Flags options for the organization */
flags: OrganizationFlags;
flags: {
/** Whether or not the organization is active */
isActive: boolean;
/** Whether or not the organization is soft deleted */
isDeleted: boolean;
/** Whether or not the organization is a demo organization */
isDemo: boolean;
};
/** Name of the organization */
name: string;
/** Slug for generating subdomains for the organization */
Expand All @@ -31,14 +26,14 @@ export interface IOrganization extends IBase<"Organization"> {
* A class the represents an organization.
*/
export class Organization extends Base<"Organization"> {
readonly domains: string[];
readonly flags: OrganizationFlags;
readonly name: string;
readonly slug: string;
readonly domains: OrganizationParameters["domains"];
readonly flags: OrganizationParameters["flags"];
readonly name: OrganizationParameters["name"];
readonly slug: OrganizationParameters["slug"];

/**
* Create an Organization entity.
* @param params An object of required and optional parameters referenced from the {@link IOrganization} interface.
* @param params An object of required and optional parameters referenced from the {@link OrganizationParameters} interface.
*/
constructor({
flags = {
Expand All @@ -55,7 +50,10 @@ export class Organization extends Base<"Organization"> {
name,
slug,
domains,
}: AtLeast<IOrganization, "createdBy" | "domains" | "name" | "slug">) {
}: AtLeast<
OrganizationParameters,
"createdBy" | "domains" | "name" | "slug"
>) {
super({
_id,
_version,
Expand All @@ -74,7 +72,7 @@ export class Organization extends Base<"Organization"> {
* Transforms the organization class object to a json object. Useful for saving the entity to the database.
* @returns a json representation of the organization.
*/
toJSON(): Omit<IOrganization, "kind"> {
toJSON(): Omit<OrganizationParameters, "kind"> {
const json = super.toJSON();
return {
...json,
Expand Down
Loading

0 comments on commit 861bb98

Please sign in to comment.