60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
|
|
/**
|
||
|
|
* OrgRedirect model - stores old org handles as redirect aliases
|
||
|
|
* When an org is renamed, the old name becomes a redirect pointing to the org.
|
||
|
|
* Redirects can be explicitly deleted by org admins.
|
||
|
|
*/
|
||
|
|
|
||
|
|
import * as plugins from '../plugins.ts';
|
||
|
|
import { db } from './db.ts';
|
||
|
|
|
||
|
|
@plugins.smartdata.Collection(() => db)
|
||
|
|
export class OrgRedirect extends plugins.smartdata.SmartDataDbDoc<OrgRedirect, OrgRedirect> {
|
||
|
|
@plugins.smartdata.unI()
|
||
|
|
public id: string = '';
|
||
|
|
|
||
|
|
@plugins.smartdata.svDb()
|
||
|
|
@plugins.smartdata.index({ unique: true })
|
||
|
|
public oldName: string = '';
|
||
|
|
|
||
|
|
@plugins.smartdata.svDb()
|
||
|
|
@plugins.smartdata.index()
|
||
|
|
public organizationId: string = '';
|
||
|
|
|
||
|
|
@plugins.smartdata.svDb()
|
||
|
|
public createdAt: Date = new Date();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a redirect from an old org name to the current org
|
||
|
|
*/
|
||
|
|
public static async create(oldName: string, organizationId: string): Promise<OrgRedirect> {
|
||
|
|
const redirect = new OrgRedirect();
|
||
|
|
redirect.id = `redirect:${oldName}`;
|
||
|
|
redirect.oldName = oldName;
|
||
|
|
redirect.organizationId = organizationId;
|
||
|
|
redirect.createdAt = new Date();
|
||
|
|
await redirect.save();
|
||
|
|
return redirect;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Find a redirect by the old name
|
||
|
|
*/
|
||
|
|
public static async findByName(name: string): Promise<OrgRedirect | null> {
|
||
|
|
return await OrgRedirect.getInstance({ oldName: name } as any);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get all redirects for an organization
|
||
|
|
*/
|
||
|
|
public static async getByOrgId(organizationId: string): Promise<OrgRedirect[]> {
|
||
|
|
return await OrgRedirect.getInstances({ organizationId } as any);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Find a redirect by ID
|
||
|
|
*/
|
||
|
|
public static async findById(id: string): Promise<OrgRedirect | null> {
|
||
|
|
return await OrgRedirect.getInstance({ id } as any);
|
||
|
|
}
|
||
|
|
}
|