feat: add snippet and test for supa db

This commit is contained in:
2024-10-21 01:31:58 +08:00
parent 089f629096
commit 0eae9458c6
8 changed files with 177 additions and 40 deletions

View File

@@ -140,44 +140,49 @@ app
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { appKey, files, version } = ctx.query.data;
if (!appKey) {
throw new CustomError('appKey is required');
}
if (!files || !files.length) {
throw new CustomError('files is required');
}
let am = await AppModel.findOne({ where: { key: appKey, uid: tokenUser.id } });
if (!am) {
am = await AppModel.create({
user: tokenUser.username,
key: appKey,
uid: tokenUser.id,
version: '0.0.0',
title: appKey,
data: {
files: [],
},
});
}
let app = await AppListModel.findOne({ where: { version: version, key: appKey, uid: tokenUser.id } });
if (!app) {
// throw new CustomError('app not found');
app = await AppListModel.create({
key: appKey,
version,
uid: tokenUser.id,
data: {
files: [],
},
});
}
const dataFiles = app.data.files || [];
const newFiles = _.uniqBy([...dataFiles, ...files], 'name');
const res = await app.update({ data: { ...app.data, files: newFiles } });
try {
const tokenUser = ctx.state.tokenUser;
const { appKey, files, version } = ctx.query.data;
if (!appKey) {
throw new CustomError('appKey is required');
}
if (!files || !files.length) {
throw new CustomError('files is required');
}
let am = await AppModel.findOne({ where: { key: appKey, uid: tokenUser.id } });
if (!am) {
am = await AppModel.create({
user: tokenUser.username,
key: appKey,
uid: tokenUser.id,
version: '0.0.0',
title: appKey,
data: {
files: [],
},
});
}
let app = await AppListModel.findOne({ where: { version: version, key: appKey, uid: tokenUser.id } });
if (!app) {
// throw new CustomError('app not found');
app = await AppListModel.create({
key: appKey,
version,
uid: tokenUser.id,
data: {
files: [],
},
});
}
const dataFiles = app.data.files || [];
const newFiles = _.uniqBy([...dataFiles, ...files], 'name');
const res = await app.update({ data: { ...app.data, files: newFiles } });
ctx.body = prefixFix(res, tokenUser.username);
ctx.body = prefixFix(res, tokenUser.username);
} catch (e) {
console.log('update error', e);
throw new CustomError(e.message);
}
})
.addTo(app);