chore: Move provisionSubdomain from Team model to teamCreator command

This commit is contained in:
Tom Moor
2022-06-22 11:09:20 +02:00
parent 88b3b50333
commit 6d4da176d1
4 changed files with 71 additions and 81 deletions

View File

@@ -21,44 +21,3 @@ describe("collectionIds", () => {
expect(response[0]).toEqual(collection.id);
});
});
describe("provisionSubdomain", () => {
it("should set subdomain if available", async () => {
const team = await buildTeam();
const subdomain = await team.provisionSubdomain("testy");
expect(subdomain).toEqual("testy");
expect(team.subdomain).toEqual("testy");
});
it("should set subdomain append if unavailable", async () => {
await buildTeam({
subdomain: "myteam",
});
const team = await buildTeam();
const subdomain = await team.provisionSubdomain("myteam");
expect(subdomain).toEqual("myteam1");
expect(team.subdomain).toEqual("myteam1");
});
it("should increment subdomain append if unavailable", async () => {
await buildTeam({
subdomain: "myteam",
});
await buildTeam({
subdomain: "myteam1",
});
const team = await buildTeam();
const subdomain = await team.provisionSubdomain("myteam");
expect(subdomain).toEqual("myteam2");
expect(team.subdomain).toEqual("myteam2");
});
it("should do nothing if subdomain already set", async () => {
const team = await buildTeam({
subdomain: "example",
});
const subdomain = await team.provisionSubdomain("myteam");
expect(subdomain).toEqual("example");
expect(team.subdomain).toEqual("example");
});
});

View File

@@ -146,35 +146,6 @@ class Team extends ParanoidModel {
);
}
// TODO: Move to command
provisionSubdomain = async function (
requestedSubdomain: string,
options = {}
) {
if (this.subdomain) {
return this.subdomain;
}
let subdomain = requestedSubdomain;
let append = 0;
for (;;) {
try {
await this.update(
{
subdomain,
},
options
);
break;
} catch (err) {
// subdomain was invalid or already used, try again
subdomain = `${requestedSubdomain}${++append}`;
}
}
return subdomain;
};
provisionFirstCollection = async (userId: string) => {
await this.sequelize!.transaction(async (transaction) => {
const collection = await Collection.create(