Skip to content

Commit

Permalink
COMPLETE?
Browse files Browse the repository at this point in the history
  • Loading branch information
omranjamal committed Aug 17, 2024
1 parent 378cdc2 commit 9d82c18
Show file tree
Hide file tree
Showing 15 changed files with 398 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const refreshTokenProcedure = publicProcedure
sub: tokenClaims.sub,
exp: Math.ceil(Date.now() / 1000) + 1 * 60 * 60, // one hour,

is_adm: tokenClaims.is_admin,
is_adm: tokenClaims.is_adm,
is_mod: tokenClaims.is_mod,
},
env.COMMON_HMAC_SECRET,
Expand All @@ -69,7 +69,7 @@ export const refreshTokenProcedure = publicProcedure
nbf: Math.ceil(Date.now() / 1000) + 1 * 55 * 60, // not valid until 5 minutes before access_token expiry.
exp: Math.ceil(Date.now() / 1000) + 60 * 24 * 60 * 60, // 60 days

is_adm: tokenClaims.is_admin,
is_adm: tokenClaims.is_adm,
is_mod: tokenClaims.is_mod,
},
env.COMMON_HMAC_SECRET,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {TRPCError} from '@trpc/server';
import {pick} from 'es-toolkit';
import {deriveStatus} from '../../../../../db/model-utils/petition.mjs';

export const listPetitionRequests = publicProcedure
export const listPetitions = publicProcedure
.input(
z.object({
filter: z.enum(['all', 'own']).default('all'),
sort: z.enum(['submission_time', 'upvotes']).default('upvotes'),
filter: z.enum(['request', 'formalized', 'own']).default('request'),
sort: z.enum(['time', 'votes']).default('votes'),
order: z.enum(['asc', 'desc']).default('desc'),
page: z.number().default(0),
}),
Expand All @@ -19,25 +19,47 @@ export const listPetitionRequests = publicProcedure
.with('results', (db) => {
return scope(
scope(
db
.selectFrom('petitions')
.leftJoin('petition_votes as upvotes', (join) =>
join
.onRef(
'petitions.id',
'=',
'upvotes.petition_id',
scope(
db
.selectFrom('petitions')
.leftJoin('petition_votes as upvotes', (join) =>
join
.onRef(
'petitions.id',
'=',
'upvotes.petition_id',
)
.on('upvotes.vote', '=', 1),
)
.select(['petitions.id'])
.select(({fn}) => [
fn
.count('upvotes.id')
.as('petition_upvote_count'),
]),
).let((query) => {
if (input.filter === 'request') {
return query
.where(
'petitions.approved_at',
'is not',
null,
)
.on('upvotes.vote', '=', 1),
)
.select(['petitions.id'])
.select(({fn}) => [
fn
.count('upvotes.id')
.as('petition_upvote_count'),
])
.where('petitions.approved_at', 'is not', null)
.where('petitions.formalized_at', 'is', null),
.where(
'petitions.formalized_at',
'is',
null,
);
} else if (input.filter === 'formalized') {
return query.where(
'petitions.formalized_at',
'is not',
null,
);
} else {
return query;
}
}),
).let((query) => {
if (input.filter === 'own') {
if (!ctx.auth?.user_id) {
Expand All @@ -59,9 +81,13 @@ export const listPetitionRequests = publicProcedure
}),
)
.let((query) =>
input.sort === 'submission_time'
input.sort === 'time'
? query.orderBy(
'petitions.submitted_at',
input.filter === 'own'
? 'petitions.created_at'
: input.filter === 'request'
? 'petitions.submitted_at'
: 'petitions.formalized_at',
input.order,
)
: query.orderBy(
Expand All @@ -71,7 +97,7 @@ export const listPetitionRequests = publicProcedure
)
.groupBy(['petitions.id'])
.offset(input.page * 32)
.limit(32);
.limit(33);
})
.with('result_with_downvotes', (db) => {
return db
Expand Down Expand Up @@ -133,6 +159,10 @@ export const listPetitionRequests = publicProcedure
},
extras: {
user_vote: petition.user_vote,
user: {
name: petition.user_name,
picture: petition.user_picture,
},
},
})),
};
Expand Down
10 changes: 10 additions & 0 deletions apps/jonogon-core/src/api/trpc/procedures/petitions/moderation.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export const approve = protectedProcedure
await ctx.services.postgresQueryBuilder
.updateTable('petitions')
.set({
rejected_at: null,
rejection_reason: null,
formalized_at: null,

approved_at: new Date(),
moderated_by: ctx.auth.user_id,
})
Expand All @@ -34,6 +38,9 @@ export const reject = protectedProcedure
await ctx.services.postgresQueryBuilder
.updateTable('petitions')
.set({
approved_at: null,
formalized_at: null,

rejected_at: new Date(),
rejection_reason: input.reason,
moderated_by: ctx.auth.user_id,
Expand All @@ -59,6 +66,9 @@ export const formalize = protectedProcedure
.set({
formalized_at: new Date(),
formalized_by: ctx.auth.user_id,

rejected_at: null,
rejection_reason: null,
})
.where('id', '=', `${input.petition_id}`)
.executeTakeFirst();
Expand Down
6 changes: 2 additions & 4 deletions apps/jonogon-core/src/api/trpc/routers/petitions.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import {
submitPetition,
updatePetition,
} from '../procedures/petitions/crud.mjs';
import {listPetitionRequests} from '../procedures/petitions/listing/petition-requests.mjs';
import {listFormalizedPetitions} from '../procedures/petitions/listing/formalized-petitions.mjs';
import {listPetitions} from '../procedures/petitions/listing/list-petitions.mjs';
import {listPendingPetitionRequests} from '../procedures/petitions/listing/pending-petition-requests.mjs';

export const petitionRouter = router({
// CRUD
listPetitionRequests: listPetitionRequests,
listFormalizedPetitions: listFormalizedPetitions,
list: listPetitions,
listPendingPetitionRequests: listPendingPetitionRequests,

get: getPetition,
Expand Down
12 changes: 6 additions & 6 deletions apps/jonogon-core/src/db/model-utils/petition.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ export function deriveStatus(petition: {
submitted_at: SelectType<Petitions['submitted_at']>;
}) {
if (petition.formalized_at) {
return 'formalized';
return 'formalized' as const;
}

if (petition.approved_at) {
return 'approved';
return 'approved' as const;
}

if (petition.rejected_at) {
return 'rejected';
return 'rejected' as const;
}

if (petition.submitted_at) {
return 'submitted';
return 'submitted' as const;
}

return 'draft';
}
return 'draft' as const;
}
2 changes: 1 addition & 1 deletion apps/jonogon-web/src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function App(props: TAppProps) {
useEffect(() => {
const timer = setTimeout(() => {
setPreloader(false);
}, 50);
}, 100);
return () => clearTimeout(timer);
}, []);

Expand Down
4 changes: 1 addition & 3 deletions apps/jonogon-web/src/app/components/custom/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ const Navigation = observer(() => {
Edit Profile
</DropdownMenuItem>
<DropdownMenuItem
onSelect={() =>
console.log('My Petitions')
}>
onSelect={() => setLocation('/?type=own')}>
My Petitions
</DropdownMenuItem>
<DropdownMenuItem
Expand Down
45 changes: 0 additions & 45 deletions apps/jonogon-web/src/app/components/custom/PetitionList.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion apps/jonogon-web/src/app/lib/date.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const formatDate = (date: Date) => {
export const formatDate = (date: Date) => {
const day = date.getDate();
const month = date.toLocaleString('default', {month: 'long'});
const year = date.getFullYear();
Expand Down
Loading

0 comments on commit 9d82c18

Please sign in to comment.