Skip to content

Commit 55e3196

Browse files
committed
web,api: Add missing setBookcaseOrder event
1 parent 702985b commit 55e3196

File tree

7 files changed

+122
-96
lines changed

7 files changed

+122
-96
lines changed

apps/web/src/composables/useDmSocket.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import type { AxiosStorage, SocketClient } from "socket-call-client";
33

44
import { type ClientEvents as AppEvents } from "~dm-services/app";
55
import { type ClientEvents as AuthEvents } from "~dm-services/auth";
6-
import {
7-
type AuthedClientEvents as UserBookcaseEvents,
8-
type ClientEvents as BookcaseEvents,
9-
} from "~dm-services/bookcase";
6+
import { type ClientEvents as BookcaseEvents } from "~dm-services/bookcase";
7+
import { type ClientEvents as BookcasePrivateEvents } from "~dm-services/bookcase/private";
108
import {
119
type AdminClientEvents as AdminBookstoreEvents,
1210
type ClientEvents as BookstoreEvents,
@@ -31,7 +29,7 @@ const defaultExport = (options: {
3129
onConnectError: (
3230
e: Error,
3331
namespace: string,
34-
eventName?: string,
32+
eventName?: string
3533
) => Promise<void> | void;
3634
onConnected?: (namespace: string) => void;
3735
session: {
@@ -71,18 +69,21 @@ const defaultExport = (options: {
7169
socket,
7270
options,
7371
publicCollection: socket.addNamespace<PublicCollectionEvents>(
74-
namespaces.PUBLIC_COLLECTION,
72+
namespaces.PUBLIC_COLLECTION
7573
),
7674
app: socket.addNamespace<AppEvents>(namespaces.APP),
7775

78-
bookcase: socket.addNamespace<BookcaseEvents>(namespaces.BOOKCASE, {
79-
session,
80-
}),
81-
userBookcase: socket.addNamespace<UserBookcaseEvents>(
76+
privateBookcase: socket.addNamespace<BookcasePrivateEvents>(
77+
namespaces.BOOKCASE_PRIVATE,
78+
{
79+
session,
80+
}
81+
),
82+
userBookcase: socket.addNamespace<BookcaseEvents>(
8283
namespaces.BOOKCASE_USER,
8384
{
8485
session,
85-
},
86+
}
8687
),
8788
stats: socket.addNamespace<StatsEvents>(namespaces.STATS, {
8889
session,
@@ -98,10 +99,10 @@ const defaultExport = (options: {
9899
namespaces.EDGECREATOR,
99100
{
100101
session,
101-
},
102+
}
102103
),
103104
presentationText: socket.addNamespace<PresentationTextEvents>(
104-
namespaces.PRESENTATION_TEXT,
105+
namespaces.PRESENTATION_TEXT
105106
),
106107
edges: socket.addNamespace<EdgesEvents>(namespaces.EDGES, {}),
107108
coa: socket.addNamespace<CoaEvents>(namespaces.COA, {
@@ -117,7 +118,7 @@ const defaultExport = (options: {
117118
storage: cacheStorage,
118119
ttl: 1000, // 1 second only, because we want to always get the latest data but still cache in case of offline
119120
},
120-
},
121+
}
121122
),
122123
userGlobalStats: socket.addNamespace<GlobalStatsUserEvents>(
123124
namespaces.GLOBAL_STATS_USER,
@@ -127,19 +128,19 @@ const defaultExport = (options: {
127128
storage: cacheStorage,
128129
ttl: 1000, // 1 second only, because we want to always get the latest data but still cache in case of offline
129130
},
130-
},
131+
}
131132
),
132133
events: socket.addNamespace<EventsEvents>(namespaces.EVENTS, {}),
133134
storySearch: storySearchSocket?.addNamespace<StorySearchEvents>(
134135
namespaces.STORY_SEARCH,
135-
{},
136+
{}
136137
),
137138
bookstore: socket.addNamespace<BookstoreEvents>(namespaces.BOOKSTORES),
138139
adminBookstore: socket.addNamespace<AdminBookstoreEvents>(
139140
namespaces.BOOKSTORES_ADMIN,
140141
{
141142
session,
142-
},
143+
}
143144
),
144145
collection: socket.addNamespace<CollectionEvents>(namespaces.COLLECTION, {
145146
session,

apps/web/src/stores/bookcase.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ export type BookcaseEdgeWithPopularity = BookcaseEdge & {
1919
export const bookcase = defineStore("bookcase", () => {
2020
const route = useRoute();
2121

22-
const { bookcase: bookcaseEvents, userBookcase: userBookcaseEvents } =
23-
inject(socketInjectionKey)!;
22+
const {
23+
privateBookcase: privateBookcaseEvents,
24+
userBookcase: userBookcaseEvents,
25+
} = inject(socketInjectionKey)!;
2426

2527
const loadedSprites = ref<{ [key: string]: string }>({}),
2628
isPrivateBookcase = ref(false),
@@ -46,16 +48,16 @@ export const bookcase = defineStore("bookcase", () => {
4648
? 0
4749
: collection().popularIssuesInCollection?.[issue.issuecode] || 0,
4850
}))) ||
49-
null,
51+
null
5052
),
5153
popularIssuesInCollectionWithoutEdge = computed(() =>
5254
bookcaseWithPopularities.value
5355
?.filter(
54-
({ edgeId, popularity }) => !edgeId && popularity && popularity > 0,
56+
({ edgeId, popularity }) => !edgeId && popularity && popularity > 0
5557
)
5658
.sort(({ popularity: popularity1 }, { popularity: popularity2 }) =>
57-
popularity2 && popularity1 ? popularity2 - popularity1 : 0,
58-
),
59+
popularity2 && popularity1 ? popularity2 - popularity1 : 0
60+
)
5961
),
6062
addLoadedSprite = ({
6163
spritePath,
@@ -71,8 +73,8 @@ export const bookcase = defineStore("bookcase", () => {
7173
},
7274
loadBookcase = async () => {
7375
if (!bookcaseContents.value) {
74-
const response = await bookcaseEvents.getBookcase(
75-
collection().user!.username,
76+
const response = await userBookcaseEvents.getBookcase(
77+
collection().user!.username
7678
);
7779
if ("error" in response) {
7880
switch (response.error) {
@@ -98,8 +100,8 @@ export const bookcase = defineStore("bookcase", () => {
98100
},
99101
loadBookcaseOptions = async () => {
100102
if (!bookcaseOptions.value) {
101-
const response = await bookcaseEvents.getBookcaseOptions(
102-
bookcaseUsername.value!,
103+
const response = await userBookcaseEvents.getBookcaseOptions(
104+
bookcaseUsername.value!
103105
);
104106
if ("error" in response) {
105107
console.error(response.error);
@@ -109,13 +111,14 @@ export const bookcase = defineStore("bookcase", () => {
109111
}
110112
},
111113
updateBookcaseOptions = async () => {
112-
await userBookcaseEvents.setBookcaseOptions(bookcaseOptions.value!);
114+
await privateBookcaseEvents.setBookcaseOptions(bookcaseOptions.value!);
113115
},
114116
loadBookcaseOrder = async () => {
115117
if (!bookcaseOrder.value) {
116-
const response = await bookcaseEvents.getBookcaseOrder(
117-
bookcaseUsername.value!,
118+
const response = await userBookcaseEvents.getBookcaseOrder(
119+
bookcaseUsername.value!
118120
);
121+
debugger;
119122
if ("error" in response) {
120123
console.error(response.error);
121124
} else {
@@ -124,8 +127,7 @@ export const bookcase = defineStore("bookcase", () => {
124127
}
125128
},
126129
updateBookcaseOrder = async () => {
127-
// TODO implement
128-
// await userBookcaseEvents.setBookcaseOrder(bookcaseOrder.value!);
130+
await privateBookcaseEvents.setBookcaseOrder(bookcaseOrder.value!);
129131
};
130132

131133
return {

packages/api/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { server as globalStats } from "./services/global-stats";
2525
import { server as globalStatsUser } from "./services/global-stats-user";
2626
import { server as presentationText } from "./services/presentation-text";
2727
import { server as publicCollection } from "./services/public-collection";
28+
import { server as privateBookcase } from "./services/bookcase/private";
2829
import { server as stats } from "./services/stats";
2930
import createSocketServer from "./socket";
3031

@@ -71,5 +72,6 @@ feedback(io);
7172
globalStats(io);
7273
globalStatsUser(io);
7374
presentationText(io);
75+
privateBookcase(io);
7476
publicCollection(io);
7577
stats(io);

packages/api/services/bookcase/index.ts

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import { prismaClient as prismaCoa } from "~prisma-schemas/schemas/coa/client";
66
import { prismaClient as prismaDm } from "~prisma-schemas/schemas/dm/client";
77

88
import type { UserServices } from "../../index";
9-
import { RequiredAuthMiddleware } from "../auth/util";
109
import namespaces from "../namespaces";
11-
import options from "./options";
1210

1311
type BookcaseEdgeRaw = Omit<BookcaseEdge, "sprites"> & {
1412
spriteName: string;
@@ -56,16 +54,27 @@ const listenEvents = ({ _socket }: UserServices<true>) => ({
5654
where: { userId },
5755
})
5856
).map(({ publicationcode }) => publicationcode);
59-
const userPublicationcodes = (
60-
await prismaDm.issue.findMany({
61-
select: {
62-
publicationcode: true,
63-
},
64-
distinct: ["publicationcode"],
65-
where: { userId },
66-
orderBy: [{ publicationcode: "asc" }],
67-
})
68-
).map(({ publicationcode }) => publicationcode);
57+
const userIssuecodes = await prismaDm.issue.findMany({
58+
select: {
59+
issuecode: true,
60+
},
61+
distinct: ["issuecode"],
62+
where: { userId },
63+
orderBy: { issuecode: "asc" },
64+
});
65+
66+
const augmentedIssuecodes =
67+
await prismaCoa.augmentIssueArrayWithInducksData(
68+
userIssuecodes.filter(
69+
(item): item is { issuecode: string } => !!item.issuecode,
70+
),
71+
);
72+
73+
const userPublicationcodes = [
74+
...new Set(
75+
augmentedIssuecodes.map(({ publicationcode }) => publicationcode),
76+
),
77+
];
6978

7079
const missingPublicationCodesInOrder = userPublicationcodes.filter(
7180
(publicationcode) =>
@@ -170,23 +179,9 @@ const listenEvents = ({ _socket }: UserServices<true>) => ({
170179
export const { client, server } = useSocketEvents<
171180
typeof listenEvents,
172181
Record<string, never>
173-
>(namespaces.BOOKCASE, {
182+
>(namespaces.BOOKCASE_USER, {
174183
listenEvents,
175184
middlewares: [],
176185
});
177186

178187
export type ClientEvents = (typeof client)["emitEvents"];
179-
180-
const authedListenEvents = (services: UserServices) => ({
181-
...options(services),
182-
});
183-
184-
export const { client: authedClient, server: authedServer } = useSocketEvents<
185-
typeof authedListenEvents,
186-
Record<string, never>
187-
>(namespaces.BOOKCASE_USER, {
188-
listenEvents: authedListenEvents,
189-
middlewares: [RequiredAuthMiddleware],
190-
});
191-
192-
export type AuthedClientEvents = (typeof authedClient)["emitEvents"];

packages/api/services/bookcase/options/index.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { prismaClient as prismaDm } from "~prisma-schemas/schemas/dm/client";
2+
3+
import type { UserServices } from "../../../index";
4+
import { useSocketEvents } from "socket-call-server";
5+
import namespaces from "../../namespaces";
6+
import { RequiredAuthMiddleware } from "../../auth/util";
7+
8+
const listenEvents = ({ _socket }: UserServices) => ({
9+
setBookcaseOptions: async ({
10+
textures,
11+
showAllCopies: showDuplicatesInBookcase,
12+
}: {
13+
textures: { bookcase: string; bookshelf: string };
14+
showAllCopies: boolean;
15+
}) => {
16+
const [, bookcaseSubTexture1] = textures.bookcase.split("/");
17+
const [, bookcaseSubTexture2] = textures.bookshelf.split("/");
18+
const user = await prismaDm.user.findUnique({
19+
where: {
20+
id: _socket.data.user.id,
21+
},
22+
});
23+
24+
if (!user) {
25+
return "User does not exist";
26+
}
27+
28+
await prismaDm.user.update({
29+
data: {
30+
...user,
31+
bookcaseSubTexture1,
32+
bookcaseSubTexture2,
33+
showDuplicatesInBookcase,
34+
},
35+
where: { id: user.id },
36+
});
37+
return "OK";
38+
},
39+
setBookcaseOrder: async (publicationCodes: string[]) => {
40+
const userId = _socket.data.user!.id;
41+
await prismaDm.bookcasePublicationOrder.deleteMany({
42+
where: { userId },
43+
});
44+
await prismaDm.bookcasePublicationOrder.createMany({
45+
data: publicationCodes.map((publicationcode, index) => ({
46+
publicationcode,
47+
order: index + 1,
48+
userId,
49+
})),
50+
});
51+
},
52+
});
53+
54+
export const { client, server } = useSocketEvents<
55+
typeof listenEvents,
56+
Record<string, never>
57+
>(namespaces.BOOKCASE_PRIVATE, {
58+
listenEvents,
59+
middlewares: [RequiredAuthMiddleware],
60+
});
61+
62+
export type ClientEvents = (typeof client)["emitEvents"];

packages/api/services/namespaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default {
22
APP: "/app",
33
AUTH: "/auth",
4-
BOOKCASE: "/bookcase",
4+
BOOKCASE_PRIVATE: "/bookcase",
55
BOOKCASE_USER: "/bookcase-user",
66
BOOKSTORES: "/bookstores",
77
BOOKSTORES_ADMIN: "/bookstores-admin",

0 commit comments

Comments
 (0)