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

Identified vote test #509

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE `agenda` ADD COLUMN `isNamed` BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN `isPublic` BOOLEAN NOT NULL DEFAULT true;
5 changes: 4 additions & 1 deletion packages/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ model Agenda {
title String
resolution String
content String
isNamed Boolean @default(false)
isPublic Boolean @default(true)
startAt DateTime? @map("start_at")
endAt DateTime? @map("end_at")
createdAt DateTime @default(now()) @map("created_at")
Expand All @@ -24,6 +26,7 @@ model Agenda {
@@map("agenda")
}


model User {
id Int @id @default(autoincrement())
username String @unique
Expand Down Expand Up @@ -122,4 +125,4 @@ enum ChatType {
notice

@@map("chat_type")
}
}
42 changes: 42 additions & 0 deletions packages/api/src/service/admin.agenda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,27 @@ const selectAgendaDefaultFields = {
},
};

const selectAgendaTypeFields = {
select: {
isNamed: true,
isPublic: true,
},
};

export const createAgenda = async ({
title,
resolution,
content,
type,
choices,
voters,
}: schema.AdminAgendaCreate) => {
const createAgendaQuery: Prisma.AgendaCreateInput = {
title,
resolution,
content,
isNamed: type.named,
isPublic: type.public,
choices: {
create: choices.map(name => ({ name })),
},
Expand All @@ -59,13 +69,18 @@ export const createAgenda = async ({
data: createAgendaQuery,
select: {
...selectAgendaDefaultFields.select,
...selectAgendaTypeFields.select,
choices: true,
voters: selectOnlyUser,
},
});

const agendaVotable: PreparingAgenda = {
...createdAgenda,
type: {
named: createdAgenda.isNamed,
public: createdAgenda.isPublic,
},
status: "preparing",
choices: createdChoices,
voters: {
Expand Down Expand Up @@ -119,6 +134,7 @@ export const startAgenda = async (agendaId: number, user: User) => {
},
select: {
...selectAgendaDefaultFields.select,
...selectAgendaTypeFields.select,
choices: {
include: {
users: true,
Expand All @@ -133,6 +149,10 @@ export const startAgenda = async (agendaId: number, user: User) => {
const ongoingAgenda: OngoingAgenda = {
...updatedAgenda,
status: "ongoing",
type: {
named: updatedAgenda.isNamed,
public: updatedAgenda.isPublic,
},
voters: {
voted: 0,
total: updatedVoters.length,
Expand Down Expand Up @@ -166,6 +186,7 @@ export const terminateAgenda = async (agendaId: number, user: User) => {
},
select: {
...selectAgendaDefaultFields.select,
...selectAgendaTypeFields.select,
choices: {
include: {
users: true,
Expand Down Expand Up @@ -195,6 +216,10 @@ export const terminateAgenda = async (agendaId: number, user: User) => {
// eslint-disable-next-line no-underscore-dangle
count: choice._count.users,
})),
type: {
named: updatedAgenda.isNamed,
public: updatedAgenda.isPublic,
},
voters: {
voted: updatedChoices.reduce(
(acc, choice) => acc + choice.users.length,
Expand Down Expand Up @@ -248,6 +273,8 @@ export const updateAgenda = async (agendaUpdate: schema.AdminAgendaUpdate) => {
data: agendaUpdate.choices.map(it => ({ name: it })),
},
},
isNamed: agendaUpdate.type.named,
isPublic: agendaUpdate.type.public,
voters: {
createMany: {
data: agendaUpdate.voters.total.map(it => ({
Expand All @@ -258,6 +285,7 @@ export const updateAgenda = async (agendaUpdate: schema.AdminAgendaUpdate) => {
},
select: {
...selectAgendaDefaultFields.select,
...selectAgendaTypeFields.select,
choices: true,
voters: selectOnlyUser,
},
Expand All @@ -278,6 +306,10 @@ export const updateAgenda = async (agendaUpdate: schema.AdminAgendaUpdate) => {
id: choice.id,
name: choice.name,
})),
type: {
named: updatedAgenda.isNamed,
public: updatedAgenda.isPublic,
},
voters: {
voted: 0,
total: voters.length,
Expand All @@ -302,6 +334,10 @@ export const updateAgenda = async (agendaUpdate: schema.AdminAgendaUpdate) => {
name: choice.name,
count: 0,
})),
type: {
named: updatedAgenda.isNamed,
public: updatedAgenda.isPublic,
},
voters: {
voted: [],
total: voters.map(voter => voter.user),
Expand Down Expand Up @@ -349,6 +385,7 @@ export const retrieveAll = async (): Promise<schema.AdminAgenda[]> => {
where: { deletedAt: null },
select: {
...selectAgendaDefaultFields.select,
...selectAgendaTypeFields.select,
startAt: true,
endAt: true,
deletedAt: true,
Expand Down Expand Up @@ -380,6 +417,11 @@ export const retrieveAll = async (): Promise<schema.AdminAgenda[]> => {
content: agenda.content,
resolution: agenda.resolution,
status,
// TODO: remove workaround
type: {
named: agenda.isNamed,
public: agenda.isPublic,
},
choices: agenda.choices.map(choice => ({
id: choice.id,
name: choice.name,
Expand Down
32 changes: 27 additions & 5 deletions packages/api/src/service/agenda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ export const retrieveAll = async (
select: {
id: true,
name: true,
users: true,
users: {
select: {
userId: true,
choiceId: true,
user: {
select: {
displayName: true,
},
},
},
},
},
},
},
Expand All @@ -35,11 +45,23 @@ export const retrieveAll = async (
title: agenda.title,
content: agenda.content,
resolution: agenda.resolution,
type: { named: agenda.isNamed, public: agenda.isPublic },
voters: {
voted: agenda.choices.reduce(
(acc, choice) => acc + choice.users.length,
0,
),
voted: agenda.isNamed
? agenda.choices.reduce(
(acc, choice) => [
...acc,
...choice.users.map(u => ({
displayName: u.user.displayName,
choiceId: u.choiceId,
})),
],
[] as { displayName: string; choiceId: number }[],
)
: agenda.choices.reduce(
(acc, choice) => acc + choice.users.length,
0,
),
total: agenda.voters.length,
},
endAt: agenda.endAt,
Expand Down
13 changes: 7 additions & 6 deletions packages/interface/src/admin/agenda/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ import { User } from "@/user";
* AdminAgendaStatus
* some description about admin agenda status type goes here
*/
export const AdminAgendaStatus = z.enum([...AgendaStatus.options, "preparing"]);
export const AdminAgendaStatus = z.enum(AgendaStatus.options);
export type AdminAgendaStatus = z.infer<typeof AdminAgendaStatus>;

/**
* AdminAgenda
* some description about admin agenda schema goes here
*/
export const AdminAgenda = AgendaBase.omit({
voters: true,
status: true,
}).extend({
export const AdminAgenda = AgendaBase.extend({
status: AdminAgendaStatus,
voters: z.object({
voted: z.array(User),
total: z.array(User),
}),
status: AdminAgendaStatus,
choices: z.array(ChoiceWithResult),
endAt: z.string(), // currently used only on terminated admin agendas
});
Expand All @@ -36,6 +33,10 @@ export const AdminAgendaCreate = z.object({
title: z.string().min(1).max(255),
content: z.string().min(1).max(255),
resolution: z.string().min(1).max(255),
type: z.object({
named: z.boolean(),
public: z.boolean(),
}),
choices: z.array(z.string().min(1).max(255)).min(1),
voters: z.object({
total: z.array(z.number()),
Expand Down
15 changes: 14 additions & 1 deletion packages/interface/src/agenda/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,22 @@ export const AgendaBase = z.object({
title: z.string(),
content: z.string(),
resolution: z.string(),
type: z.object({
named: z.boolean(),
public: z.boolean(),
}),
status: z.never(), // Must be overridden
voters: z.object({
voted: z.number(),
voted: z.union([
z.number(),
z.array(
z.object({
// userId: z.number(),
displayName: z.string(),
choiceId: z.number(),
}),
),
]),
total: z.number(),
}),
});
Expand Down
60 changes: 60 additions & 0 deletions packages/web/src/components/atoms/Toggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import styled from "@emotion/styled";
import { css } from "@emotion/react";

type Size = number | "hug" | "fill";
type Position = "left" | "middle" | "right";

const calcSize = (size: Size) => {
if (size === "fill") return "fill";
if (size === "hug") return "fit-content";
return `${size}px`;
};
const calcBorderRadius = (position: Position) => {
if (position === "left") return "5px 0px 0px 5px";
if (position === "middle") return "0px 0px 0px 0px";
if (position === "right") return "0px 5px 5px 0px";
return "0px 0px 0px 0px";
};

export const ToggleContainor = styled.div<{
w?: Size;
h?: Size;
}>(
({ w = "fill", h = 20 }) => css`
display: flex;
width: ${calcSize(w)};
height: ${calcSize(h)};
border: none;
justify-content: center;
align-items: center;
overflow: hidden;
`,
);

export const ToggleButton = styled.div<{
selected: boolean;
position: Position;
}>(
({ selected, position, theme }) => css`
display: flex;
width: fill;
height: 100%;
border-style: solid;
border-width: 1px;
border-radius: ${calcBorderRadius(position)};
border-color: ${selected ? theme.colors.blue500 : theme.colors.gray300};
border-left-width: ${position === "left" || selected ? "1px" : "0px"};
border-right-width: ${position === "right" || selected ? "1px" : "0px"};
justify-content: center;
align-items: center;
padding: 4px 5px 4px 5px;
background-color: ${selected ? theme.colors.blue200 : theme.colors.white};
transition-duration: 0.25s;
transition-property: background-color;

&:hover {
cursor: ${selected ? "default" : "pointer"};
background-color: ${theme.colors.blue200};
}
`,
);
1 change: 1 addition & 0 deletions packages/web/src/components/atoms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from "./TaggersBox";
export * from "./HyperText";
export * from "./PresetOption";
export * from "./TagSelect";
export * from "./Toggle";
Loading
Loading