Skip to content

Commit

Permalink
Create new product (partial)
Browse files Browse the repository at this point in the history
BuildEngine is being a pain, again.
Pushing so there is a record for repro efforts.
  • Loading branch information
FyreByrd committed Oct 7, 2024
1 parent 8f72c57 commit ccedead
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export async function request(
body?: any
) {
const { url, token } = await getURLandToken(organizationId);
console.log(`request: ${JSON.stringify(body)}`); // TODO: remove
return await fetch(`${url}/${resource}`, {
method: method,
headers: {
Expand Down Expand Up @@ -90,7 +91,10 @@ export async function createJob(
organizationId: number,
job: Types.JobConfig
): Promise<Types.JobResponse | Types.ErrorResponse> {
console.log(`Requests.createJob job: ${JSON.stringify(job, null, 4)}`); // TODO: remove
const res = await request('job', organizationId, 'POST', job);
console.log(`Requests.createJob res: ${res.status}: ${res.statusText}`); // TODO: remove
console.log(`Requests.createJob res: ${JSON.stringify(await res.json(), null, 4)}`); // TODO: remove
return res.ok
? ((await res.json()) as Types.JobResponse)
: ((await res.json()) as Types.ErrorResponse);
Expand Down
1 change: 1 addition & 0 deletions source/SIL.AppBuilder.Portal/common/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ model ProductBuilds {
@@index([ProductId], map: "IX_ProductBuilds_ProductId")
}

// TODO: Can we please rework/consolidate the ProductDefinitions and WorkflowDefinitions tables and add some useful information for S2? I can make it work for now, but we should eventually consider how to rework some of the tables as we move away from DWKit and its associated idiosyncracies.
model ProductDefinitions {
Id Int @id(map: "PK_ProductDefinitions") @default(autoincrement())
Name String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,115 @@ const updateOwnerGroupSchema = v.object({
const addProductSchema = v.object({
productDefinitionId: idSchema,
storeId: idSchema,
storeLanguageId: idSchema
storeLanguageId: v.nullable(idSchema)
});

// Are we sending too much data?
// Maybe? I pared it down a bit with `select` instead of `include` - Aidan
export const load = (async ({ locals, params }) => {
if (!verifyCanViewAndEdit((await locals.auth())!, parseInt(params.id))) return error(403);
const project = await prisma.projects.findUnique({
where: {
Id: parseInt(params.id)
},
include: {
ApplicationType: true,
select: {
Id: true,
Name: true,
Description: true,
WorkflowProjectUrl: true,
IsPublic: true,
AllowDownloads: true,
DateCreated: true,
Language: true,
ApplicationType: {
select: {
Description: true
}
},
Organization: {
select: {
Id: true
}
},
Products: {
include: {
select: {
Id: true,
DateUpdated: true,
DatePublished: true,
ProductDefinition: {
include: {
Workflow: true
select: {
Id: true,
Name: true
}
},
UserTasks: true,
Store: true
// Probably don't need to optimize this. Unless it's a really large org, there probably won't be very many of these records for an individual product. In most cases, there will only be zero or one. The only times there will be more is if it's an admin task or an author task.
UserTasks: {
select: {
DateCreated: true,
UserId: true
}
},
Store: {
select: {
Description: true
}
}
}
},
Owner: {
select: {
Id: true,
Name: true
}
},
Group: {
select: {
Id: true,
Abbreviation: true
}
},
Owner: true,
Group: true,
Authors: {
include: {
Users: true
select: {
Id: true,
Users: {
select: {
Id: true,
Name: true
}
}
}
},
Reviewers: true
Reviewers: {
select: {
Id: true,
Name: true,
Email: true
}
}
}
});
if (!project) return error(400);

const organization = await prisma.organizations.findUnique({
where: {
Id: project.Organization.Id
},
select: {
OrganizationStores: {
select: {
Store: {
select: {
Id: true,
Name: true,
Description: true,
StoreTypeId: true
}
}
}
}
}
})

const transitions = await prisma.productTransitions.findMany({
where: {
ProductId: {
Expand All @@ -88,17 +163,43 @@ export const load = (async ({ locals, params }) => {
where: {
GroupMemberships: {
some: {
GroupId: project?.GroupId
GroupId: project?.Group.Id
}
},
UserRoles: {
some: {
OrganizationId: project?.OrganizationId,
OrganizationId: project?.Organization.Id,
RoleId: RoleId.Author
}
}
}
});

const productDefinitions = (await prisma.organizationProductDefinitions.findMany({
where: {
OrganizationId: project.Organization.Id,
ProductDefinition: {
ApplicationTypes: project.ApplicationType
}
},
select: {
ProductDefinition: {
select: {
Id: true,
Name: true,
Description: true,
Workflow: {
select: {
StoreTypeId: true
}
}
}
}
}
})).map((pd) => pd.ProductDefinition);

const projectProductDefinitionIds = project.Products.map((p) => p.ProductDefinition.Id);

const authorForm = await superValidate(valibot(addAuthorSchema));
const reviewerForm = await superValidate({ language: 'en-us' }, valibot(addReviewerSchema));
return {
Expand All @@ -108,32 +209,35 @@ export const load = (async ({ locals, params }) => {
...product,
Transitions: transitions.filter((t) => t.ProductId === product.Id),
PreviousTransition: strippedTransitions.find(
(t) => (t[0] ?? t[1]).ProductId === product.Id
(t) => (t[0] ?? t[1])?.ProductId === product.Id
)?.[0],
ActiveTransition: strippedTransitions.find(
(t) => (t[0] ?? t[1]).ProductId === product.Id
(t) => (t[0] ?? t[1])?.ProductId === product.Id
)?.[1]
}))
},
possibleProjectOwners: await prisma.users.findMany({
where: {
OrganizationMemberships: {
some: {
OrganizationId: project.OrganizationId
OrganizationId: project.Organization.Id
}
}
}
}),
possibleGroups: await prisma.groups.findMany({
where: {
OwnerId: project.OrganizationId
OwnerId: project.Organization.Id
}
}),
authorsToAdd,
authorForm,
reviewerForm,
deleteAuthorForm: await superValidate(valibot(deleteAuthorSchema)),
deleteReviewerForm: await superValidate(valibot(deleteReviewerSchema))
deleteReviewerForm: await superValidate(valibot(deleteReviewerSchema)),
productsToAdd: productDefinitions.filter((pd) => !projectProductDefinitionIds.includes(pd.Id)),
addProductForm: await superValidate(valibot(addProductSchema)),
stores: organization?.OrganizationStores.map((os) => os.Store) ?? []
};
}) satisfies PageServerLoad;

Expand All @@ -151,6 +255,7 @@ export const actions = {
return fail(403);
const form = await superValidate(event.request, valibot(deleteAuthorSchema));
if (!form.valid) return fail(400, { form, ok: false });
// TODO: Update user tasks...
await DatabaseWrites.authors.delete({ where: { Id: form.data.id } });
return { form, ok: true };
},
Expand All @@ -169,15 +274,15 @@ export const actions = {
async addProduct(event) {
if (!verifyCanViewAndEdit((await event.locals.auth())!, parseInt(event.params.id)))
return fail(403);
// TODO: api and bulltask
const form = await superValidate(event.request, valibot(addProductSchema));
if (!form.valid) return fail(400, { form, ok: false });
console.log(JSON.stringify(form, null, 4));
// Appears that CanUpdate is not used TODO
const productId = await DatabaseWrites.products.create({
ProjectId: parseInt(event.params.id),
ProductDefinitionId: form.data.productDefinitionId,
StoreId: form.data.storeId,
StoreLanguageId: form.data.storeLanguageId,
StoreLanguageId: form.data.storeLanguageId ?? undefined,
WorkflowJobId: 0,
WorkflowBuildId: 0,
WorkflowPublishId: 0
Expand Down
Loading

0 comments on commit ccedead

Please sign in to comment.