Skip to content

Commit 5ceda5e

Browse files
committed
fix pinned announcement not showing in latest
1 parent 9cea323 commit 5ceda5e

4 files changed

Lines changed: 44 additions & 3 deletions

File tree

api/src/controllers/announcement.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,45 @@ export const getPinnedAnnouncements = async (req, res, next) => {
225225
}
226226
}
227227

228+
export const getLatestAnnouncement = async (req, res, next) => {
229+
try {
230+
const [announcement] = await Announcement.aggregate([
231+
{
232+
$sort: { created: -1 },
233+
},
234+
{
235+
$limit: 1,
236+
},
237+
{
238+
$lookup: {
239+
from: 'users',
240+
as: 'createdBy',
241+
let: { userId: '$createdBy' },
242+
pipeline: [
243+
{
244+
$match: { $expr: { $eq: ['$_id', '$$userId'] } },
245+
},
246+
{
247+
$project: {
248+
username: 1,
249+
},
250+
},
251+
],
252+
},
253+
},
254+
{
255+
$unwind: {
256+
path: '$createdBy',
257+
preserveNullAndEmptyArrays: true,
258+
},
259+
},
260+
])
261+
res.json(announcement)
262+
} catch (e) {
263+
next(e)
264+
}
265+
}
266+
228267
export const deleteAnnouncement = async (req, res, next) => {
229268
try {
230269
if (req.userRole !== 'admin') {

api/src/routes/announcement.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import {
88
getAnnouncements,
99
getPinnedAnnouncements,
1010
pinAnnouncement,
11+
getLatestAnnouncement,
1112
} from '../controllers/announcement'
1213

1314
const router = express.Router()
1415

1516
export default () => {
1617
router.post('/new', createAnnouncement)
1718
router.get('/pinned', getPinnedAnnouncements)
19+
router.get('/latest', getLatestAnnouncement)
1820
router.get('/:slug', fetchAnnouncement)
1921
router.get('/page/:page', getAnnouncements)
2022
router.delete('/:slug', deleteAnnouncement)

client/components/List.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const List = ({ data = [], columns = [], ...rest }) => {
9191
width="100%"
9292
display="flex"
9393
alignItems="center"
94-
textAlign={col.rightAlign ? ['left', 'right'] : 'left'}
94+
textAlign={col.rightAlign ? 'right' : 'left'}
9595
_css={{
9696
overflow: 'hidden',
9797
'> *': {

client/pages/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ export const getServerSideProps = withAuthServerSideProps(
168168
const latestTorrents = await latestTorrentsRes.json()
169169

170170
const latestAnnouncementRes = await fetch(
171-
`${SQ_API_URL}/announcements/page/0?count=1`,
171+
`${SQ_API_URL}/announcements/latest`,
172172
{
173173
headers: fetchHeaders,
174174
}
175175
)
176-
const [latestAnnouncement] = await latestAnnouncementRes.json()
176+
const latestAnnouncement = await latestAnnouncementRes.json()
177177

178178
const verifiedRes = await fetch(`${SQ_API_URL}/account/get-verified`, {
179179
headers: fetchHeaders,

0 commit comments

Comments
 (0)