Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make owner optional in experiment POST endpoint. #2133

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/back-end/generated/spec.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 25 additions & 17 deletions packages/back-end/src/api/experiments/postExperiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const postExperiment = createApiRequestHandler(postExperimentValidator)(
async (req): Promise<PostExperimentResponse> => {
req.checkPermissions("createAnalyses", req.body.project);

const { datasourceId, owner } = req.body;
const { datasourceId, owner: ownerEmail } = req.body;

const datasource = await getDataSourceById(req.context, datasourceId);

Expand Down Expand Up @@ -47,20 +47,26 @@ export const postExperiment = createApiRequestHandler(postExperimentValidator)(
);
}

const user = await getUserByEmail(owner);
const ownerId = await (async () => {
if (!ownerEmail) return req.context.userId;

// check if the user is a member of the organization
const isMember = req.organization.members.some(
(member) => member.id === user?.id
);
const user = await getUserByEmail(ownerEmail);

if (!isMember || !user) {
throw new Error(`Unable to find user: ${owner}.`);
}
// check if the user is a member of the organization
const isMember = req.organization.members.some(
(member) => member.id === user?.id
);

if (!isMember || !user) {
throw new Error(`Unable to find user: ${ownerEmail}.`);
}

return user.id;
})();

// transform into exp interface; set sane defaults
const newExperiment = postExperimentApiPayloadToInterface(
{ ...req.body, owner: user.id },
{ ...req.body, ...(ownerId ? { owner: ownerId } : {}) },
req.organization,
datasource
);
Expand All @@ -70,13 +76,15 @@ export const postExperiment = createApiRequestHandler(postExperimentValidator)(
context: req.context,
});

// add owner as watcher
await upsertWatch({
userId: user.id,
organization: req.organization.id,
item: experiment.id,
type: "experiments",
});
if (ownerId) {
// add owner as watcher
await upsertWatch({
userId: ownerId,
organization: req.organization.id,
item: experiment.id,
type: "experiments",
});
}

const apiExperiment = await toExperimentApiInterface(
req.context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ type: object
required:
- name
- variations
- owner
- datasourceId
- trackingKey
- assignmentQueryId
Expand Down
2 changes: 1 addition & 1 deletion packages/back-end/src/validators/openapi.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/back-end/types/openapi.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading