my-kaiqiuwang/__test__/location.test.ts
kyuuseiryuu fd8257e194 feat(api): add user location management endpoints and database model
- Added `UserLocation` model to `prisma/schema.prisma` to store user location data including coordinates (point geometry), name, avatar, and bindings to Logto and Kaiqiu users.
- Created new API endpoints under `/api/account/location` to handle location operations:
  - `POST`: Create a new location record for a user.
  - `GET`: Retrieve nearby locations based on latitude and longitude.
  - `PUT`: Update an existing location record, including coordinate updates via spatial index.
  - `DELETE`: Remove a specific location record.
- Integrated the custom spatial operations from `xprisma` DAO to support geospatial queries and updates.
- Updated dependencies to include `@mui/icons-material` (and its peer dependencies like `prop-types` and `react-transition-group` which are implicitly required by the new icon components).
- Implemented `verifyLogtoToken` logic to secure the new endpoints and associate locations with authenticated users.

No breaking changes in API contracts; new endpoints are additive.
2026-03-19 00:29:02 +09:00

52 lines
2.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { test, expect } from 'bun:test';
import { prisma } from '../src/prisma/db';
import xprisma from '../src/dao/xprisma';
test('Test add location', async () => {
const lat = 39.9042; // 纬度
const lng = 116.4074; // 经度
// 注意MySQL 8.0 推荐顺序是 POINT(经度 纬度)
const logto_uid = 'test';
const result = await xprisma.userLocation.createCustom(logto_uid, '北京天安门', { lat, lng });
const result2 = await xprisma.userLocation.createCustom(logto_uid, '北京天安门2', { lat: lat + 0.005, lng: lng + 0.007 });
const result3 = await xprisma.userLocation.createCustom(logto_uid, 'test 2', { lat: 40.33, lng: 88.555 });
expect(result).toBeDefined();
expect(result2).toBeDefined();
expect(result3).toBeDefined();
const locations = await xprisma.userLocation.findManyWihtLocation(logto_uid);
const data = {
kaiqiu_uid: '1234',
avatar: 'https://p3-sign.douyinpic.com/obj/douyin-user-image-file/3da637dde522b3a4bc0c5e8be5bea173?lk3s=7b078dd2&x-expires=1773838800&x-signature=mmUrSnUnta67cH%2B5rgBJXzdp4IA%3D&from=2064092626&s=sticker_comment&se=false&sc=sticker_heif&biz_tag=aweme_comment&l=20260318155244DA8F89CAF9B7CB54B2A9',
// avatar: 'avatar 111',
};
const ids = locations.map(e => e.id);
await prisma.userLocation.updateMany({
where: { id: { in: ids }},
data,
});
const id = ids[0] as number;
const updateNoMatch = await prisma.userLocation.update({
where: { id: id, logto_uid: 'no match' },
data: { avatar: '222' }
}).catch(() => null);
expect(updateNoMatch).toBe(null);
console.debug('updatematch', updateNoMatch);
await xprisma.userLocation.updateLocation(id, {
lat: lat - 0.001,
lng: lng + 0.001,
});
expect(locations.length).toBe(3);
const neerby = await xprisma.userLocation.findNearby({
lat: lat - 0.001,
lng: lng - 0.003,
}, 500).catch();
console.debug('location', locations);
console.debug('neerby', neerby);
expect(neerby).toBeArray();
await prisma.userLocation.deleteMany({
where: {
id: { in: locations.map(e => e.id) },
}
});
});