-
Notifications
You must be signed in to change notification settings - Fork 0
/
materials.ts
213 lines (169 loc) · 5.48 KB
/
materials.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { OpenAPIHono } from '@hono/zod-openapi';
import { supabase } from '../libs/supabase.js';
import { zodErrorHook } from '../libs/zodError.js';
import {
addMaterial,
changeMaterialQuantity,
createMaterial,
deleteMaterial,
getAllMaterials,
getMaterialById,
removeMaterial,
updateMaterial,
} from '../routes/materials.js';
import { checkRole } from '../utils/context.js';
import type { Variables } from '../validators/general.js';
import { Role } from '../validators/general.js';
export const materials = new OpenAPIHono<{ Variables: Variables }>({
defaultHook: zodErrorHook,
});
materials.openapi(getAllMaterials, async (c) => {
const { all, search, addresses } = c.req.valid('query');
const user = c.get('user');
const roles = user.roles;
await checkRole(roles, false, [Role.ADMIN]);
let materialIds: number[] | null = null;
if (!all) {
const { data: addressData, error: addressError } = await supabase
.from('ADDRESSES_MATERIALS')
.select('id_material')
.in('id_address', addresses);
if (addressError) {
return c.json({ error: addressError.message }, 500);
}
materialIds = addressData.map((ad) => ad.id_material);
}
const query = supabase
.from('MATERIALS')
.select('*, addresses:ADDRESSES_MATERIALS (id_address, quantity)', { count: 'exact' })
.order('id', { ascending: true });
if (search) {
query.ilike('name', `%${search}%`);
}
if (materialIds) {
query.in('id', materialIds);
}
const { data, error, count } = await query;
if (error) {
return c.json({ error: error.message }, 500);
}
const formattedData = data.map((material) => {
const { addresses, ...rest } = material;
return {
...rest,
addresses: addresses,
};
});
const responseData = {
data: formattedData || [],
count: count || 0,
};
return c.json(responseData, 200);
});
materials.openapi(getMaterialById, async (c) => {
const { id } = c.req.valid('param');
const user = c.get('user');
const roles = user.roles;
await checkRole(roles, false);
const { data, error } = await supabase
.from('MATERIALS')
.select('*, addresses:ADDRESSES_MATERIALS (id_address, quantity)')
.eq('id', id)
.single();
if (error || !data || !data.addresses[0]) {
return c.json({ error: 'Material not found' }, 404);
}
const formattedData = {
id: data.id,
name: data.name,
weight_grams: data.weight_grams,
addresses: data.addresses,
};
return c.json(formattedData, 200);
});
materials.openapi(createMaterial, async (c) => {
const { name, weight_grams } = c.req.valid('json');
const user = c.get('user');
const roles = user.roles;
await checkRole(roles, false);
const { data, error } = await supabase.from('MATERIALS').insert([{ name, weight_grams }]).select().single();
if (error) {
return c.json({ error: error.message }, 500);
}
return c.json(data, 201);
});
materials.openapi(updateMaterial, async (c) => {
const { id } = c.req.valid('param');
const { name, weight_grams } = c.req.valid('json');
const user = c.get('user');
const roles = user.roles;
await checkRole(roles, false);
const { data, error } = await supabase
.from('MATERIALS')
.update({ name, weight_grams })
.eq('id', id)
.select()
.single();
if (error || !data) {
return c.json({ error: 'Material not found' }, 404);
}
return c.json(data, 200);
});
materials.openapi(deleteMaterial, async (c) => {
const { id } = c.req.valid('param');
const user = c.get('user');
const roles = user.roles;
await checkRole(roles, false);
const { error, count } = await supabase.from('MATERIALS').delete({ count: 'exact' }).eq('id', id);
if (error || count === 0) {
return c.json({ error: 'Material not found' }, 404);
}
return c.json({ message: 'Material deleted' }, 200);
});
materials.openapi(changeMaterialQuantity, async (c) => {
const { id } = c.req.valid('param');
const { quantity, id_address } = c.req.valid('json');
const user = c.get('user');
const roles = user.roles;
await checkRole(roles, false);
const { error } = await supabase
.from('ADDRESSES_MATERIALS')
.update({ quantity })
.eq('id_material', id)
.eq('id_address', id_address);
if (error) {
return c.json({ error: "Material or address doesn't exist" }, 404);
}
return c.json({ message: 'Quantity updated' }, 200);
});
materials.openapi(addMaterial, async (c) => {
const { id } = c.req.valid('param');
const { id_address, quantity } = c.req.valid('json');
const user = c.get('user');
const roles = user.roles;
await checkRole(roles, false);
const { error } = await supabase.from('ADDRESSES_MATERIALS').insert([{ id_material: id, id_address, quantity }]);
if (error && error.code === '23505') {
return c.json({ error: 'Material already exists in this address' }, 409);
}
if (error) {
return c.json({ error: "Material or address doesn't exist" }, 404);
}
return c.json({ message: 'Material added' }, 201);
});
materials.openapi(removeMaterial, async (c) => {
const { id } = c.req.valid('param');
const { id_address } = c.req.valid('json');
const user = c.get('user');
const roles = user.roles;
await checkRole(roles, false);
const { error, count } = await supabase
.from('ADDRESSES_MATERIALS')
.delete({ count: 'exact' })
.eq('id_material', id)
.eq('id_address', id_address);
if (error || count === 0) {
return c.json({ error: 'Material not found' }, 404);
}
return c.json({ message: 'Material removed' }, 200);
});