- Added SettingsComponent for user profile management, including display name and password change functionality. - Introduced TokensComponent for managing API tokens, including creation and revocation. - Created LayoutComponent for consistent application layout with navigation and user information. - Established main application structure in index.html and main.ts. - Integrated Tailwind CSS for styling and responsive design. - Configured TypeScript settings for strict type checking and module resolution.
110 lines
2.7 KiB
TypeScript
110 lines
2.7 KiB
TypeScript
/**
|
|
* OrganizationMember model - links users to organizations with roles
|
|
*/
|
|
|
|
import * as plugins from '../plugins.ts';
|
|
import type { IOrganizationMember, TOrganizationRole } from '../interfaces/auth.interfaces.ts';
|
|
import { getDb } from './db.ts';
|
|
|
|
@plugins.smartdata.Collection(() => getDb())
|
|
export class OrganizationMember
|
|
extends plugins.smartdata.SmartDataDbDoc<OrganizationMember, OrganizationMember>
|
|
implements IOrganizationMember
|
|
{
|
|
@plugins.smartdata.unI()
|
|
public id: string = '';
|
|
|
|
@plugins.smartdata.svDb()
|
|
@plugins.smartdata.index()
|
|
public organizationId: string = '';
|
|
|
|
@plugins.smartdata.svDb()
|
|
@plugins.smartdata.index()
|
|
public userId: string = '';
|
|
|
|
@plugins.smartdata.svDb()
|
|
@plugins.smartdata.index()
|
|
public role: TOrganizationRole = 'member';
|
|
|
|
@plugins.smartdata.svDb()
|
|
public invitedBy?: string;
|
|
|
|
@plugins.smartdata.svDb()
|
|
public joinedAt: Date = new Date();
|
|
|
|
@plugins.smartdata.svDb()
|
|
@plugins.smartdata.index()
|
|
public createdAt: Date = new Date();
|
|
|
|
/**
|
|
* Add a member to an organization
|
|
*/
|
|
public static async addMember(data: {
|
|
organizationId: string;
|
|
userId: string;
|
|
role: TOrganizationRole;
|
|
invitedBy?: string;
|
|
}): Promise<OrganizationMember> {
|
|
// Check if member already exists
|
|
const existing = await OrganizationMember.getInstance({
|
|
organizationId: data.organizationId,
|
|
userId: data.userId,
|
|
});
|
|
|
|
if (existing) {
|
|
throw new Error('User is already a member of this organization');
|
|
}
|
|
|
|
const member = new OrganizationMember();
|
|
member.id = await OrganizationMember.getNewId();
|
|
member.organizationId = data.organizationId;
|
|
member.userId = data.userId;
|
|
member.role = data.role;
|
|
member.invitedBy = data.invitedBy;
|
|
member.joinedAt = new Date();
|
|
member.createdAt = new Date();
|
|
await member.save();
|
|
return member;
|
|
}
|
|
|
|
/**
|
|
* Find membership for user in organization
|
|
*/
|
|
public static async findMembership(
|
|
organizationId: string,
|
|
userId: string
|
|
): Promise<OrganizationMember | null> {
|
|
return await OrganizationMember.getInstance({
|
|
organizationId,
|
|
userId,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get all members of an organization
|
|
*/
|
|
public static async getOrgMembers(organizationId: string): Promise<OrganizationMember[]> {
|
|
return await OrganizationMember.getInstances({
|
|
organizationId,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get all organizations a user belongs to
|
|
*/
|
|
public static async getUserOrganizations(userId: string): Promise<OrganizationMember[]> {
|
|
return await OrganizationMember.getInstances({
|
|
userId,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Lifecycle hook
|
|
*/
|
|
public async beforeSave(): Promise<void> {
|
|
if (!this.id) {
|
|
this.id = await OrganizationMember.getNewId();
|
|
}
|
|
}
|
|
}
|