Skip to content

Commit

Permalink
no longer using 22 digit barcodes :)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgodha24 committed Sep 22, 2023
1 parent 6a0748b commit 24e6d7c
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 73 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@aws-sdk/client-dynamodb": "^3.398.0",
"@trpc/server": "^10.37.1",
"electrodb": "^2.9.1",
"nanoid": "^5.0.1",
"superjson": "^1.13.1",
"ulid": "^2.3.0",
"zod": "^3.22.2"
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/db/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TRPCError } from "@trpc/server";
type ClassInner = EntityRecord<typeof ClassEntity>;
type Student = ClassInner["students"][number];

export async function byUserID(userID: string) {
export async function byUserID(userID: number) {
const classes = await ClassEntity.query.classes({ userID }).go();
return classes.data;
}
Expand All @@ -16,7 +16,7 @@ export async function byID({
userID,
}: {
classID: string;
userID: string;
userID: number;
}) {
const class_ = await ClassEntity.query.classes({ classID, userID }).go();
return class_.data[0];
Expand All @@ -37,7 +37,7 @@ export async function addStudent({
student,
}: {
classID: string;
userID: string;
userID: number;
student: Student;
}) {
// this should (?) make sure that the user owns the class because it's looking to patch
Expand All @@ -55,7 +55,7 @@ export async function removeStudent({
studentID,
}: {
classID: string;
userID: string;
userID: number;
studentID: number;
}) {
const oldStudents = await byID({ classID, userID });
Expand All @@ -77,7 +77,7 @@ export async function deleteClass({
userID,
}: {
classID: string;
userID: string;
userID: number;
}) {
// should also check that the user owns the class bc pk(userID)
const class_ = await ClassEntity.delete({ classID, userID }).go();
Expand Down
28 changes: 22 additions & 6 deletions packages/core/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ export const UserEntity = new Entity(
service: "attendance",
},
attributes: {
userID: {
googleID: {
type: "string",
required: true,
},
userID: {
type: "number",
required: true,
},
email: {
type: "string",
required: true,
Expand All @@ -29,8 +33,20 @@ export const UserEntity = new Entity(
},
},
indexes: {
byId: {
collection: "attendance",
byGoogleID: {
collection: "google_user",
index: "gsi1",
pk: {
field: "gsi1pk",
composite: ["googleID"],
},
sk: {
field: "gsi1sk",
composite: [],
},
},
byUserID: {
collection: "uid",
pk: {
field: "pk",
composite: ["userID"],
Expand Down Expand Up @@ -58,7 +74,7 @@ export const ClassEntity = new Entity(
required: true,
},
userID: {
type: "string",
type: "number",
required: true,
},
students: {
Expand Down Expand Up @@ -135,7 +151,7 @@ export const SignInEntity = new Entity(
model: { entity: "SignIn", version: "1", service: "attendance" },
attributes: {
userID: {
type: "string",
type: "number",
required: true,
},
studentID: {
Expand Down Expand Up @@ -193,7 +209,7 @@ export const ConnectionEntity = new Entity(
required: true,
},
userID: {
type: "string",
type: "number",
required: true,
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/db/signin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function create(args: Omit<SignInInner, "time" | "signInID">) {
}

export async function get(
{ userID, scannerName }: { userID: string; scannerName?: string },
{ userID, scannerName }: { userID: number; scannerName?: string },
dates?: { start: Date; end?: Date }
) {
const res = await SignInEntity.query
Expand Down
36 changes: 27 additions & 9 deletions packages/core/src/db/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,50 @@ export async function create(args: UserInner) {
return res.data;
}

export async function get({ userID }: { userID: string }) {
const res = await UserEntity.get({ userID }).go();
export async function getUID({ userID }: { userID: number }) {
const res = await UserEntity.query.byUserID({ userID }).go();

return res.data;
return res.data.length === 1 ? res.data[0] : null;
}

export async function getGoogleID({ googleID }: { googleID: string }) {
const res = await UserEntity.query.byGoogleID({ googleID }).go();

return res.data.length === 1 ? res.data[0] : null;
}

export async function addScanner(userID: string, name: string) {
console.log("Adding scanner", name, "to uid", userID);
export async function addScanner({
userID,
scannerName,
}: {
userID: number;
scannerName: string;
}) {
console.log("Adding scanner", scannerName, "to uid", userID);
const res = await UserEntity.update({
userID,
})
.add({
connectedScanners: [name],
connectedScanners: [scannerName],
})
.go();

return res.data;
}

export async function removeScanner(userID: string, scanner: string) {
console.log("removing scanner", scanner, "from uid", userID);
export async function removeScanner({
userID,
scannerName,
}: {
userID: number;
scannerName: string;
}) {
console.log("removing scanner", scannerName, "from uid", userID);
const res = await UserEntity.update({
userID,
})
.delete({
connectedScanners: [scanner],
connectedScanners: [scannerName],
})
.go();

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/message/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Config } from "sst/node/config";
import superjson from "superjson";
import { Message } from "./schema";

export async function sendMessage(userID: string, message: Message) {
export async function sendMessage(userID: number, message: Message) {
const wsURL = Config.wsURL.replace("wss://", "https://");
const manager = new ApiGatewayManagementApi({ endpoint: wsURL });

Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/uid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { customAlphabet } from "nanoid";

let nanoid = customAlphabet("0123456789", 15);

export function createUID() {
let num: number;

do {
num = parseInt(nanoid());
} while (num.toString().length !== 15);

return num;
}
1 change: 1 addition & 0 deletions packages/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@aws-sdk/client-apigatewaymanagementapi": "^3.398.0",
"@trpc/server": "^10.37.1",
"fast-jwt": "^3.2.0",
"nanoid": "^5.0.1",
"zod": "^3.22.2"
}
}
16 changes: 12 additions & 4 deletions packages/functions/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AuthHandler, GoogleAdapter, Session } from "sst/node/auth";
import { Config } from "sst/node/config";
import { User } from "@attendance/core/db/user";
import { createUID } from "@attendance/core/uid";

export const handler = AuthHandler({
providers: {
Expand All @@ -14,10 +15,15 @@ export const handler = AuthHandler({

console.log("here!!!");

const user = await User.get({ userID: sub });
let user = await User.getGoogleID({ googleID: sub });
if (!user) {
console.log("creating user");
await User.create({ userID: sub, email, connectedScanners: [""] });
user = await User.create({
googleID: sub,
userID: createUID(),
email,
connectedScanners: [""],
});
} else {
console.log("using alr made user w/ email", email, user);
}
Expand All @@ -26,7 +32,8 @@ export const handler = AuthHandler({
type: "user",
redirect: Config.frontendURL,
properties: {
userID: claims.sub,
userID: user.userID,
googleID: user.googleID,
},
});
},
Expand All @@ -37,7 +44,8 @@ export const handler = AuthHandler({
declare module "sst/node/auth" {
export interface SessionTypes {
user: {
userID: string;
userID: number;
googleID: string;
};
}
}
12 changes: 7 additions & 5 deletions packages/functions/src/changeScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { User } from "@attendance/core/db/user";

export const handler = ApiHandler(async (req) => {
const schema = z.object({
oldUID: z.string().min(20).optional(),
newUID: z.string().min(20).optional(),
oldUID: z.coerce.number().optional(),
newUID: z.coerce.number().optional(),
scannerName: z.string().min(2),
});

Expand All @@ -30,7 +30,7 @@ export const handler = ApiHandler(async (req) => {
);

promises.push(
User.removeScanner(oldUID, scannerName).catch((err) =>
User.removeScanner({ scannerName, userID: oldUID }).catch((err) =>
console.error(
"unable to remove scanner",
scannerName,
Expand All @@ -53,7 +53,7 @@ export const handler = ApiHandler(async (req) => {
);

promises.push(
User.addScanner(newUID, scannerName).catch((err) =>
User.addScanner({ scannerName, userID: newUID }).catch((err) =>
console.error(
"unable to add scanner",
scannerName,
Expand All @@ -66,7 +66,9 @@ export const handler = ApiHandler(async (req) => {
}
}

await Promise.all(promises);
const p = await Promise.all(promises);

console.log(p.length);

return { statusCode: 200, body: "OK" };
});
5 changes: 4 additions & 1 deletion packages/functions/src/signin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export const handler = ApiHandler(async (req) => {
const schema = z.object({
studentID: z.coerce.number().min(10_000).max(99_999),
scannerName: z.coerce.string(),
userID: z.string().min(6),
userID: z.coerce
.number()
.min(1 * 10 ** 14)
.max(1 * 10 ** 15),
});

const data = schema.safeParse(req.queryStringParameters);
Expand Down
2 changes: 1 addition & 1 deletion packages/trpc/src/routers/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { User } from "@attendance/core/db/user";

export const scannerRouter = router({
connected: privateProcedure.query(async ({ ctx: { userID } }) => {
const user = await User.get({ userID });
const user = await User.getUID({ userID });
if (!user) throw new TRPCError({ code: "UNAUTHORIZED" });
return user.connectedScanners;
}),
Expand Down
4 changes: 3 additions & 1 deletion packages/trpc/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import superjson from "superjson";
declare module "sst/node/auth" {
export interface SessionTypes {
user: {
userID: string;
userID: number;
googleID: string;
};
}
}
Expand All @@ -27,6 +28,7 @@ const ensureAuthedMiddleware = t.middleware(({ ctx, next }) => {
...ctx,
session: ctx.session,
userID: ctx.session.properties.userID,
googleID: ctx.session.properties.googleID,
},
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const Navbar = () => {
setTheme(theme === "dark" ? "light" : "dark");
}}
id="themeSwitcher"
aria-label="theme switcher"
>
{theme === "dark" ? <Sun /> : <Moon />}
</Button>
Expand Down
8 changes: 4 additions & 4 deletions packages/web/src/components/selectors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const ScannerSelect: FC<{ scanners: string[] }> = ({ scanners }) => {
initialData: [],
});

if (!uid) redirect({ to: "/login", replace: true });
if (!uid) return redirect({ to: "/login", replace: true });
return (
<Dialog>
<Select
Expand Down Expand Up @@ -101,17 +101,17 @@ export const ScannerSelect: FC<{ scanners: string[] }> = ({ scanners }) => {
src={
"https://api.qrserver.com/v1/create-qr-code?" +
new URLSearchParams({
data: "https://barcodeapi.org/api/codabar/" + uid,
data: "https://barcodeapi.org/api/128/" + uid.toString(36),
size: "300x300",
}).toString()
})
}
alt="qrcode"
/>
<p className="text-center">
scan this qrcode on your phone, then scan the barcode it generates
on the scanner to connect to it
{import.meta.env.DEV && "\n" + uid}
</p>
{import.meta.env.DEV && <p>{uid.toString(36)}</p>}
</div>
</DialogContent>
</DialogOverlay>
Expand Down
Loading

0 comments on commit 24e6d7c

Please sign in to comment.