Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
🐛 Fix org creation method
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Apr 7, 2020
1 parent 159df7b commit da31292
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
16 changes: 3 additions & 13 deletions src/rest/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
} from "../helpers/jwt";
import { mail } from "../helpers/mail";
import { trackEvent } from "../helpers/tracking";
import { dnsResolve } from "../helpers/utils";
import { dnsResolve, organizationUsernameToId } from "../helpers/utils";
import { queueWebhook } from "../helpers/webhooks";
import { OrgScopes, Templates, Webhooks, Tokens } from "../interfaces/enum";
import { KeyValue, Locals } from "../interfaces/general";
Expand Down Expand Up @@ -85,6 +85,7 @@ import {
getApiKeyLogs,
checkDomainAvailability,
getOrganizationById,
createOrganization,
} from "../services/organization.service";
import { fireSingleWebhook } from "../helpers/webhooks";
import { getUserById } from "../services/user.service";
Expand All @@ -110,18 +111,7 @@ export const newOrganizationForUser = async (
const user = await getUserById(userId);
organization.name = user.name;
}
return prisma.organizations.create({
data: {
...organization,
memberships: {
create: {
organization: {},
user: { connect: { id: parseInt(userId) } },
role: "OWNER",
},
},
},
});
return createOrganization(organization, userId);
};

export const updateOrganizationForUser = async (
Expand Down
16 changes: 14 additions & 2 deletions src/services/organization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ const getBestUsernameForOrganization = async (name: string) => {
* Create a new organization for a user
*/
export const createOrganization = async (
organization: organizationsCreateInput
organization: organizationsCreateInput,
ownerId: string
) => {
if (!organization.name) throw new Error(INVALID_INPUT);
organization.name = capitalizeFirstAndLastLetter(organization.name);
Expand All @@ -84,7 +85,18 @@ export const createOrganization = async (
organization.profilePicture = `https://ui-avatars.com/api/?name=${encodeURIComponent(
(organization.name || "XX").substring(0, 2).toUpperCase()
)}&background=${backgroundColor}&color=fff`;
const result = await prisma.organizations.create({ data: organization });
const result = await prisma.organizations.create({
data: {
...organization,
memberships: {
create: {
organization: {},
user: { connect: { id: parseInt(ownerId) } },
role: "OWNER",
},
},
},
});
return result;
};

Expand Down
1 change: 0 additions & 1 deletion src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ export const getUserById = async (id: number | string) => {
try {
return await getItemFromCache<users>(key);
} catch (error) {
console.log("Reached error");
const user = await prisma.users.findOne({ where: { id: parseInt(id) } });
if (user) {
await setItemInCache(key, user);
Expand Down

0 comments on commit da31292

Please sign in to comment.