Skip to content
Open
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
28 changes: 28 additions & 0 deletions backend/db_migrations/000021_duplicate-meal-name-input.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
revoke execute on function app.duplicate_meal_plan(bigint,bigint,text) from app_admin, app_meal_designer, app_user;
drop function if exists app.duplicate_meal_plan(bigint,bigint,text); --otherwise it creates two separate functions
comment on column app.meal_plan_entry.days is 'MONDAY: 0, TUESDAY: 1.., SUNDAY: 6';
create or replace function app.duplicate_meal_plan(mealplan_id bigint, p_id bigint) returns app.meal_plan as $$
declare
m app.meal_plan;
entry_ids bigint[];
begin
--create a duplicate meal plan with a different meal plan id and person id p_id but the same contents
INSERT INTO app.meal_plan (name_en, name_fr, person_id, description_en, description_fr, tags)
SELECT name_en, name_fr, p_id as person_id, description_en, description_fr, tags FROM app.meal_plan WHERE id=mealplan_id
RETURNING * INTO m;
-- m = UPDATE app.meal_plan SET person_id=p_id WHERE id = m.id RETURNING *;

--create duplicate of all meal plan entries associated with the meal_plan_id

INSERT INTO app.meal_plan_entry (category, days, meal_plan_id, meal_id)
SELECT category, days, m.id AS meal_plan_id, meal_id FROM app.meal_plan_entry
WHERE meal_plan_id = mealplan_id;

return m;

end;

$$ language plpgsql;

comment on function app.duplicate_meal_plan(bigint, bigint) is 'Duplicate meal plan by meal designer or admin';
GRANT execute on function app.duplicate_meal_plan(bigint, bigint) to app_admin, app_meal_designer;
38 changes: 38 additions & 0 deletions backend/db_migrations/000021_duplicate-meal-name-input.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

comment on column app.meal_plan_entry.days is 'MONDAY: 0, TUESDAY: 1.., SUNDAY: 6';
revoke execute on function app.duplicate_meal_plan(bigint,bigint) from app_admin, app_meal_designer, app_user;
drop function if exists app.duplicate_meal_plan(bigint,bigint); --otherwise it creates two separate functions
create or replace function app.duplicate_meal_plan(mealplan_id bigint, p_id bigint, dup_name_en text) returns app.meal_plan as $$
declare
m app.meal_plan;
entry_ids bigint[];
p app.person;
begin
p := app.current_user_person(app.current_person());

--create a duplicate meal plan with a different meal plan id and person id p_id but the same contents
IF (p.id = p_id and p.role='app_user') THEN
INSERT INTO app.meal_plan (name_en, name_fr, person_id, description_en, description_fr, tags)
SELECT dup_name_en as name_en, name_fr, p_id as person_id, description_en, description_fr, tags FROM app.meal_plan WHERE id=mealplan_id
RETURNING * INTO m;
ELSE
INSERT INTO app.meal_plan (name_en, name_fr, person_id, description_en, description_fr, tags)
SELECT dup_name_en as name_en, name_fr, null, description_en, description_fr, tags FROM app.meal_plan WHERE id=mealplan_id
RETURNING * INTO m;
END IF;
-- m = UPDATE app.meal_plan SET person_id=p_id WHERE id = m.id RETURNING *;

--create duplicate of all meal plan entries associated with the meal_plan_id

INSERT INTO app.meal_plan_entry (category, days, meal_plan_id, meal_id)
SELECT category, days, m.id AS meal_plan_id, meal_id FROM app.meal_plan_entry
WHERE meal_plan_id = mealplan_id;

return m;

end;

$$ language plpgsql;

comment on function app.duplicate_meal_plan(bigint, bigint,text) is 'Duplicate meal plan by meal designer, admin, or user';
GRANT execute on function app.duplicate_meal_plan(bigint, bigint,text) to app_admin, app_meal_designer, app_user;
7 changes: 4 additions & 3 deletions mealplanner-ui/src/pages/MealPlans/DuplicateMealPlan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { commitMutation } from "relay-runtime";
import environment from "../../relay/environment";

const duplicateMealPlanGQL = graphql`
mutation DuplicateMealPlanMutation($connections: [ID!]!, $mealPlanId: BigInt!, $personId:BigInt!) {
duplicateMealPlan(input: {mealplanId: $mealPlanId, personId: $personId}) {
mutation DuplicateMealPlanMutation($connections: [ID!]!, $mealPlanId: BigInt!, $personId:BigInt!, $duplicateNameEn:String!) {
duplicateMealPlan(input: {mealplanId: $mealPlanId, pId: $personId, dupNameEn: $duplicateNameEn}) {
mealPlanEdge @prependEdge(connections: $connections) {
cursor
node {
Expand Down Expand Up @@ -34,13 +34,14 @@ mutation DuplicateMealPlanMutation($connections: [ID!]!, $mealPlanId: BigInt!, $
`;


export const duplicateMealPlan = (connection: string, id:string, pId:string) => {
export const duplicateMealPlan = (connection: string, id:string, pId:string, dupNameEn:string) => {
commitMutation(environment, {
mutation: duplicateMealPlanGQL,
variables: {
connections: [connection],
mealPlanId: id.toString(),
personId: pId.toString(),
duplicateNameEn: dupNameEn.toString(),
},
onCompleted(response, errors) {
console.log(response);
Expand Down
2 changes: 1 addition & 1 deletion mealplanner-ui/src/pages/MealPlans/MealPlanCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const MealPlanCard = (props: MealPlanCardProps) => {
aria-label="duplicate"
onClick={(e) => {
e.stopPropagation();
duplicateMealPlan(connection, mealplan.rowId,getCurrentPerson().personID);
duplicateMealPlan(connection, mealplan.rowId,getCurrentPerson().personID, mealplan.nameEn.concat(" copy"));
}}
sx={{ "& :hover": { color: theme.palette.primary.main } }}
>
Expand Down
Loading